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; }

preferences:
98.56 ms | 1286 KiB | 5 Q