3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $inventory = array( array("fruit"=>"orange", "price"=>3), array("fruit"=>"kiwi", "price"=>2), array("fruit"=>"apple", "price"=>3), array("fruit"=>"apple", "price"=>3), array("fruit"=>"apple", "price"=>3), array("fruit"=>"orange", "price"=>4), array("fruit"=>"banana", "price"=>10), array("fruit"=>"banana", "price"=>10), ); function groupFruits(&$result, $item) { $key = $item['fruit']; @$result[$key] += $item['price']; return $result; } $grouped = array_reduce($inventory, 'groupFruits', array()); arsort($grouped); print_r($grouped);
Output for 4.3.0 - 4.3.2, 5.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [banana] => 20 [apple] => 9 [orange] => 7 [kiwi] => 2 )
Output for 4.3.10 - 4.4.9, 5.0.3 - 5.2.17
Warning: arsort() expects parameter 1 to be array, integer given in /in/EuCmT on line 26 0
Output for 4.3.3 - 4.3.9, 5.0.0 - 5.0.2
Array ( [banana] => 20 [apple] => 9 [orange] => 7 [kiwi] => 55 )