3v4l.org

run code in 300+ PHP versions simultaneously
<?php $rawData = array( array( 'sku' => 'product1', 'quantity' => '3' ), array( 'sku' => 'product2', 'quantity' => '3' ), array( 'sku' => 'product1', 'quantity' => '2' ) ); $collectedData = array(); foreach ($rawData as $value) { $collectedData[$value['sku']] += $value['quantity']; } print_r($collectedData);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Warning: Undefined array key "product1" in /in/5WtiB on line 20 Warning: Undefined array key "product2" in /in/5WtiB on line 20 Array ( [product1] => 5 [product2] => 3 )
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined index: product1 in /in/5WtiB on line 20 Notice: Undefined index: product2 in /in/5WtiB on line 20 Array ( [product1] => 5 [product2] => 3 )
Output for 7.3.32 - 7.3.33
Array ( [product1] => 5 [product2] => 3 )

preferences:
132.85 ms | 408 KiB | 5 Q