3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ ['code'=>'aaa','name'=>'a','qty'=>20,'price'=>12.00], ['code'=>'bbb','name'=>'b','qty'=>20,'price'=>12.00], ['code'=>'ccc','name'=>'c','qty'=>20,'price'=>12.00], ['code'=>'aaa','name'=>'a','qty'=>20,'price'=>12.00], ['code'=>'ddd','name'=>'d','qty'=>20,'price'=>12.00], ['code'=>'ccc','name'=>'c','qty'=>5,'price'=>8.00], ['code'=>'bbb','name'=>'b','qty'=>15,'price'=>10.00], ['code'=>'ggg','name'=>'g','qty'=>20,'price'=>12.00] ]; $newdata = array_reduce(array_slice($data, 1), function ($c, $v) { if (($k = array_search($v['code'], array_column($c, 'code'))) !== false) { $c[$k]['qty'] += $v['qty']; $c[$k]['price'] += $v['price']; } else { $c[] = $v; } return $c; }, array($data[0])); print_r($newdata);

preferences:
180.78 ms | 408 KiB | 5 Q