I have the following array
Array ( [id] => 1 [name] => [cat_name] => Food & Beverage [quantity] => 1
[price] => 25 [image] => books_image/calories_fat_carbohydrate.png )
Array ( [id] => 2 [name] => [cat_name] => Food & Beverage [quantity] => 5
[price] => 38 [image] => books_image/the_law_relating_to_food.png )
Array ( [id] => 3 [name] => [cat_name] => Food & Beverage [quantity] => 5
[price] => 19 [image] => books_image/it_starts_with_food.png )
Array ( [id] => 3 [name] => [cat_name] => Food & Beverage [quantity] => 2
[price] => 19 [image] => books_image/it_starts_with_food.png )
In a $_SESSION['cartitem'] object, and the key is id
.
My problem is that i have duplicates with id=3
and my desired output would be
Array ( [id] => 1 [name] => [cat_name] => Food & Beverage [quantity] => 1
[price] => 25 [image] => books_image/calories_fat_carbohydrate.png )
Array ( [id] => 2 [name] => [cat_name] => Food & Beverage [quantity] => 5
[price] => 38 [image] => books_image/the_law_relating_to_food.png )
Array ( [id] => 3 [name] => [cat_name] => Food & Beverage [quantity] => 7
[price] => 19 [image] => books_image/it_starts_with_food.png )
Basically merging by same id
and adding the quantity
field. Is there an easy way to do this in php?
I dont want to remove duplicates. The duplicates to me are important since they have quantities in which i need to add them.