Skip to main content
se eliminaron 19 caracteres en el cuerpo; título editado
Origen Enlace

MultiArray discard duplicated key values with conditiondescartar duplicados según condicion

I have an Array Like thisTengo un array como este

$ratesData = [
    [1] => [
        'id' => 1,
        'amount' => 2
    ],
    [0] => [
        'id' => 1,
        'amount' => 1
    ],
    [2] => [
        'id' => 1,
        'amount' => 3
    ],
    [3] => [
        'id' => 2,
        'amount' => 2
    ]
]

I want to keep the duplicated id arrays with cheapestQuiero mantener los ids duplicados con el menor amount, the result will be like thisel resultado será como este

[
    [0] => [
       'id' => 1,
       'amount' => 1
    ],
    [1] => [
       'id' => 2,
       'amount' => 2
    ]
]

I have a code that works with this problem, but I'm searching an elegant way to accomplish this without all this loopsTengo un código que funciona pero estaba buscando una forma mas elegante de conseguir el objetivo

    foreach($ratesData as $firstLoopKey => $firstLoopValue) {
        foreach($ratesData as $secondLoopKey => $secondLoopValue) {
            if($firstLoopValue['id'] === $secondLoopValue['id'] && $firstLoopKey != $secondLoopKey ) {
                if ($ratesData[$secondLoopKey]['total_amount'] > $ratesData[$firstLoopKey]['total_amount']) {
                    $deleteElements[] = $secondLoopKey;
                }
            }
        }
    }

    if (isset($deleteElements)) {
        foreach ($deleteElements as $element) {
            unset($ratesData[$element]);
        }
    }

    $ratesData = array_values($ratesData);

    return $ratesData;

Gracias

MultiArray discard duplicated key values with condition

I have an Array Like this

$ratesData = [
    [1] => [
        'id' => 1,
        'amount' => 2
    ],
    [0] => [
        'id' => 1,
        'amount' => 1
    ],
    [2] => [
        'id' => 1,
        'amount' => 3
    ],
    [3] => [
        'id' => 2,
        'amount' => 2
    ]
]

I want to keep the duplicated id arrays with cheapest amount, the result will be like this

[
    [0] => [
       'id' => 1,
       'amount' => 1
    ],
    [1] => [
       'id' => 2,
       'amount' => 2
    ]
]

I have a code that works with this problem, but I'm searching an elegant way to accomplish this without all this loops

    foreach($ratesData as $firstLoopKey => $firstLoopValue) {
        foreach($ratesData as $secondLoopKey => $secondLoopValue) {
            if($firstLoopValue['id'] === $secondLoopValue['id'] && $firstLoopKey != $secondLoopKey ) {
                if ($ratesData[$secondLoopKey]['total_amount'] > $ratesData[$firstLoopKey]['total_amount']) {
                    $deleteElements[] = $secondLoopKey;
                }
            }
        }
    }

    if (isset($deleteElements)) {
        foreach ($deleteElements as $element) {
            unset($ratesData[$element]);
        }
    }

    $ratesData = array_values($ratesData);

    return $ratesData;

MultiArray descartar duplicados según condicion

Tengo un array como este

$ratesData = [
    [1] => [
        'id' => 1,
        'amount' => 2
    ],
    [0] => [
        'id' => 1,
        'amount' => 1
    ],
    [2] => [
        'id' => 1,
        'amount' => 3
    ],
    [3] => [
        'id' => 2,
        'amount' => 2
    ]
]

Quiero mantener los ids duplicados con el menor amount, el resultado será como este

[
    [0] => [
       'id' => 1,
       'amount' => 1
    ],
    [1] => [
       'id' => 2,
       'amount' => 2
    ]
]

Tengo un código que funciona pero estaba buscando una forma mas elegante de conseguir el objetivo

    foreach($ratesData as $firstLoopKey => $firstLoopValue) {
        foreach($ratesData as $secondLoopKey => $secondLoopValue) {
            if($firstLoopValue['id'] === $secondLoopValue['id'] && $firstLoopKey != $secondLoopKey ) {
                if ($ratesData[$secondLoopKey]['total_amount'] > $ratesData[$firstLoopKey]['total_amount']) {
                    $deleteElements[] = $secondLoopKey;
                }
            }
        }
    }

    if (isset($deleteElements)) {
        foreach ($deleteElements as $element) {
            unset($ratesData[$element]);
        }
    }

    $ratesData = array_values($ratesData);

    return $ratesData;

Gracias

se añadieron 7 caracteres en el cuerpo
Origen Enlace

I have an Array Like this

$ratesData = [
    [0][1] => [
        'id' => 1,
        'amount' => 12
        ],
    [1][0] => [
        'id' => 1,
        'amount' => 21
        ],
    [2] => [
        'id' => 1,
        'amount' => 3
    ],
    [3] => [
        'id' => 2,
        'amount' => 2
    ]
]

I want to keep the duplicated id arrays with cheapest amount, the result will be like this

[
    [0] => [
       'id' => 1,
       'amount' => 1
    ],
    [1] => [
       'id' => 2,
       'amount' => 2
    ]
]

I have a code that works with this problem, but I'm searching an elegant way to accomplish this without all this loops

    foreach($ratesData as $firstLoopKey => $firstLoopValue) {
        foreach($ratesData as $secondLoopKey => $secondLoopValue) {
            if($firstLoopValue['id'] === $secondLoopValue['id'] && $firstLoopKey != $secondLoopKey ) {
                if ($ratesData[$secondLoopKey]['total_amount'] > $ratesData[$firstLoopKey]['total_amount']) {
                    $deleteElements[] = $secondLoopKey;
                }
            }
        }
    }

    if (isset($deleteElements)) {
        foreach ($deleteElements as $element) {
            unset($ratesData[$element]);
        }
    }

    $ratesData = array_values($ratesData);

    return $ratesData;

I have an Array Like this

$ratesData = [
    [0] => [
        'id' => 1,
        'amount' => 1
        ]
    [1] => [
        'id' => 1,
        'amount' => 2
        ]
    [2] => [
        'id' => 1,
        'amount' => 3
    ]
    [3] => [
        'id' => 2,
        'amount' => 2
    ]
]

I want to keep the duplicated id arrays with cheapest amount, the result will be like this

[
    [0] => [
       'id' => 1,
       'amount' => 1
    ]
    [1] => [
       'id' => 2,
       'amount' => 2
]

I have a code that works with this problem, but I'm searching an elegant way to accomplish this without all this loops

    foreach($ratesData as $firstLoopKey => $firstLoopValue) {
        foreach($ratesData as $secondLoopKey => $secondLoopValue) {
            if($firstLoopValue['id'] === $secondLoopValue['id'] && $firstLoopKey != $secondLoopKey ) {
                if ($ratesData[$secondLoopKey]['total_amount'] > $ratesData[$firstLoopKey]['total_amount']) {
                    $deleteElements[] = $secondLoopKey;
                }
            }
        }
    }

    if (isset($deleteElements)) {
        foreach ($deleteElements as $element) {
            unset($ratesData[$element]);
        }
    }

    $ratesData = array_values($ratesData);

    return $ratesData;

I have an Array Like this

$ratesData = [
    [1] => [
        'id' => 1,
        'amount' => 2
    ],
    [0] => [
        'id' => 1,
        'amount' => 1
    ],
    [2] => [
        'id' => 1,
        'amount' => 3
    ],
    [3] => [
        'id' => 2,
        'amount' => 2
    ]
]

I want to keep the duplicated id arrays with cheapest amount, the result will be like this

[
    [0] => [
       'id' => 1,
       'amount' => 1
    ],
    [1] => [
       'id' => 2,
       'amount' => 2
    ]
]

I have a code that works with this problem, but I'm searching an elegant way to accomplish this without all this loops

    foreach($ratesData as $firstLoopKey => $firstLoopValue) {
        foreach($ratesData as $secondLoopKey => $secondLoopValue) {
            if($firstLoopValue['id'] === $secondLoopValue['id'] && $firstLoopKey != $secondLoopKey ) {
                if ($ratesData[$secondLoopKey]['total_amount'] > $ratesData[$firstLoopKey]['total_amount']) {
                    $deleteElements[] = $secondLoopKey;
                }
            }
        }
    }

    if (isset($deleteElements)) {
        foreach ($deleteElements as $element) {
            unset($ratesData[$element]);
        }
    }

    $ratesData = array_values($ratesData);

    return $ratesData;
Origen Enlace

MultiArray discard duplicated key values with condition

I have an Array Like this

$ratesData = [
    [0] => [
        'id' => 1,
        'amount' => 1
        ]
    [1] => [
        'id' => 1,
        'amount' => 2
        ]
    [2] => [
        'id' => 1,
        'amount' => 3
    ]
    [3] => [
        'id' => 2,
        'amount' => 2
    ]
]

I want to keep the duplicated id arrays with cheapest amount, the result will be like this

[
    [0] => [
       'id' => 1,
       'amount' => 1
    ]
    [1] => [
       'id' => 2,
       'amount' => 2
]

I have a code that works with this problem, but I'm searching an elegant way to accomplish this without all this loops

    foreach($ratesData as $firstLoopKey => $firstLoopValue) {
        foreach($ratesData as $secondLoopKey => $secondLoopValue) {
            if($firstLoopValue['id'] === $secondLoopValue['id'] && $firstLoopKey != $secondLoopKey ) {
                if ($ratesData[$secondLoopKey]['total_amount'] > $ratesData[$firstLoopKey]['total_amount']) {
                    $deleteElements[] = $secondLoopKey;
                }
            }
        }
    }

    if (isset($deleteElements)) {
        foreach ($deleteElements as $element) {
            unset($ratesData[$element]);
        }
    }

    $ratesData = array_values($ratesData);

    return $ratesData;