3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arrays = [ ['id' => 1, 'name' => '', 'cat_name' => 'Food & Beverage', 'quantity' => 1, 'price' => 25, 'image' => 'books_image/calories_fat_carbohydrate.png'], ['id' => 2, 'name' => '', 'cat_name' => 'Food & Beverage', 'quantity' => 5, 'price' => 38, 'image' => 'books_image/the_law_relating_to_food.png'], ['id' => 3, 'name' => '', 'cat_name' => 'Food & Beverage', 'quantity' => 5, 'price' => 19, 'image' => 'books_image/it_starts_with_food.png'], ['id' => 1, 'name' => '', 'cat_name' => 'Food & Beverage', 'quantity' => 2, 'price' => 19, 'image' => 'books_image/it_starts_with_food.png'] ]; $result = array_values(array_reduce($arrays, function($result, $entry) { if (isset($result[$entry['id']])) { $result[$entry['id']]['quantity'] += $entry['quantity']; } else { $result[$entry['id']] = $entry; } return $result; }, [])); var_dump($result);
Output for 5.6.38, 7.1.0 - 7.1.24, 7.2.0 - 7.2.33, 7.3.16 - 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(3) { [0]=> array(6) { ["id"]=> int(1) ["name"]=> string(0) "" ["cat_name"]=> string(15) "Food & Beverage" ["quantity"]=> int(3) ["price"]=> int(25) ["image"]=> string(41) "books_image/calories_fat_carbohydrate.png" } [1]=> array(6) { ["id"]=> int(2) ["name"]=> string(0) "" ["cat_name"]=> string(15) "Food & Beverage" ["quantity"]=> int(5) ["price"]=> int(38) ["image"]=> string(40) "books_image/the_law_relating_to_food.png" } [2]=> array(6) { ["id"]=> int(3) ["name"]=> string(0) "" ["cat_name"]=> string(15) "Food & Beverage" ["quantity"]=> int(5) ["price"]=> int(19) ["image"]=> string(35) "books_image/it_starts_with_food.png" } }
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:
196.67 ms | 408 KiB | 5 Q