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 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
Array ( [000001] => Array ( [Id] => 000001 [Quantity] => 40 ) [000002] => Array ( [Id] => 000002 [Quantity] => 29 ) [000003] => Array ( [Id] => 000003 [Quantity] => 40 ) )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
161.3 ms | 407 KiB | 5 Q