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);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => Array ( [code] => aaa [name] => a [qty] => 40 [price] => 24 ) [1] => Array ( [code] => bbb [name] => b [qty] => 35 [price] => 22 ) [2] => Array ( [code] => ccc [name] => c [qty] => 25 [price] => 20 ) [3] => Array ( [code] => ddd [name] => d [qty] => 20 [price] => 12 ) [4] => Array ( [code] => ggg [name] => g [qty] => 20 [price] => 12 ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
173.56 ms | 407 KiB | 5 Q