3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sumArray($array, $index, $col) { $returnArray = []; // temporary container // sanity checks if (!is_array($array)) { return 'error not an array'; } $firstRow = reset($array); if (!array_key_exists($index, $firstRow) || !array_key_exists($col, $firstRow)) { return 'error keys provided not found'; } foreach ($array as $value) { if (!isset($returnArray[$value[$index]])) { // initialize $returnArray[$value[$index]] = [$index => $value[$index], $col => 0]; } // add value $returnArray[$value[$index]][$col] += $value[$col]; } return $returnArray; } $products = array ( array("Id" => "000001", "Name" => "Cheese", "Quantity" => "10", "Price" => "10"), array("Id" => "000001", "Name" => "Cheese", "Quantity" => "20", "Price" => "20"), array("Id" => "000001", "Name" => "Cheese", "Quantity" => "10", "Price" => "30"), array("Id" => "000002", "Name" => "Ham", "Quantity" => "20", "Price" => "200"), array("Id" => "000002", "Name" => "Ham", "Quantity" => "9", "Price" => "100"), array("Id" => "000003", "Name" => "Baicon", "Quantity" => "40", "Price" => "900") ); $summedArray = sumArray($products, 'Id', 'Quantity'); print_r($summedArray);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [000001] => Array ( [Id] => 000001 [Quantity] => 40 ) [000002] => Array ( [Id] => 000002 [Quantity] => 29 ) [000003] => Array ( [Id] => 000003 [Quantity] => 40 ) )

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:
28.71 ms | 406 KiB | 5 Q