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