3v4l.org

run code in 500+ PHP versions simultaneously
<?php $data = [ [ 'id' => 2, 'order_id' => 1, 'adm_aftr_disc' => 25.5, ], [ 'id' => 4, 'order_id' => 2, 'adm_aftr_disc' => 10, ], [ 'id' => 6, 'order_id' => 1, 'adm_aftr_disc' => 50, ], ]; print_r(groupData($data)); function groupData(array $data) { // Our return array $ret = []; foreach($data as $item) { // This is what we're grouping on $orderId = $item['order_id']; // If it doesn't exist in the return array, add it with sane defaults if(!array_key_exists($orderId, $ret)){ $ret[$orderId] = [ 'order_id' => $orderId, 'adm_aftr_disc' => 0, ]; } // Sum. Add more as needed $ret[$orderId]['adm_aftr_disc'] += $item['adm_aftr_disc']; } return $ret; }
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.7
Array ( [1] => Array ( [order_id] => 1 [adm_aftr_disc] => 75.5 ) [2] => Array ( [order_id] => 2 [adm_aftr_disc] => 10 ) )

preferences:
104.34 ms | 1286 KiB | 4 Q