3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $basket = [ [ 'name' => 'apple', 'qty' => 5, ], [ 'name' => 'apple', 'qty' => 4, ], [ 'name' => 'pear', 'qty' => 4, ] ]; // need a function that would convert this to $output = []; foreach ($basket as $item) { $key = $item['name']; if (!isset($output[$key])) $output[$key] = 0; $output[$key] += $item['qty']; } var_dump($output); //$output = [['name' => 'apple', 'qty' => 9], ['name' => 'pear', 'qty' => 4]];
Output for 5.6.38 - 7.3.1
array(2) { ["apple"]=> int(9) ["pear"]=> int(4) }