1

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.

Jack Logan
  • 95
  • 8
  • 2
    Iterate over session array and when you found required Id - add quantity. Also you can use ID as array key then it will be faster. – u_mulder Nov 26 '18 at 20:24
  • Possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – cch Nov 26 '18 at 21:00

1 Answers1

1

Here you go:

// DEFINE SORTING CRITERIA
function sort_ids($a, $b) {
    return $a['id'] > $b['id'];
}
// DEFINE ARRAY OF ARRAYS
$arrays = 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' ),

    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' )
);
// SORT ARRAY OF ARRAYS FIRST
usort($arrays, 'sort_ids');
// MERGE DUPLICATES
$record_number = count($arrays);
for ($i=0; $i<$record_number; $i++) {
    // if this is not the first loop and if this is a duplicate
    if(isset($temp_id) && $temp_id == $arrays[$i]['id']) {
        // add quantity of the previous array here
        $arrays[$i]['quantity'] += $temp_quantity;
        // unset previous array
        unset($arrays[$i-1]);
    }
    $temp_id = $arrays[$i]['id'];
    $temp_quantity = $arrays[$i]['quantity'];
}
// PRINT ARRAY OF ARRAYS
echo '<pre>';
print_r($arrays);
echo '</pre>';
Adem Tepe
  • 564
  • 5
  • 10
  • 1
    This is not exactly duplicate, he wants to add quantities sir! – Adem Tepe Nov 26 '18 at 21:10
  • Your code seemed to be working, but I have found that when the "duplicates" dont appear in $i and $i+1 it doesnt work. If I have duplicate in $i and then in $i+3, it's not working at all since you are only unsetting previous array. Could you help me ? – Jack Logan Nov 26 '18 at 23:21
  • @JackLogan I have revised it. Can you please accept as accepted answer if it works for you? – Adem Tepe Nov 29 '18 at 12:42
  • No problem, I'm glad it helped. – Adem Tepe Nov 29 '18 at 22:24