<?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);
You have javascript disabled. You will not be able to edit any code.