0

I have this source array():

$source[]

[
     ["user_id": 1, "item_id": 991, "quantity": 100],
     ["user_id": 1, "item_id": 992, "quantity": 50],
     ["user_id": 1, "item_id": 993, "quantity": 300],
     ["user_id": 1, "item_id": 992, "quantity": 150],
     ["user_id": 2, "item_id": 991, "quantity": 75],
     ["user_id": 2, "item_id": 992, "quantity": 20],
     ["user_id": 3, "item_id": 991, "quantity": 200],
     ["user_id": 3, "item_id": 992, "quantity": 425],
     ["user_id": 3, "item_id": 991, "quantity": 100],
     ["user_id": 3, "item_id": 992, "quantity": 75]
]  

My goal is to reduce it so that i get only 1 item where user_id & item_id are the same:

$result[]

[
     ["user_id": 1, "item_id": 991, "quantity": 100],
     ["user_id": 1, "item_id": 992, "quantity": 200],
     ["user_id": 1, "item_id": 993, "quantity": 300],
     ["user_id": 2, "item_id": 991, "quantity": 75],
     ["user_id": 2, "item_id": 992, "quantity": 20],
     ["user_id": 3, "item_id": 991, "quantity": 300],
     ["user_id": 3, "item_id": 992, "quantity": 500]
]  

The ultimate way would be to use an array_reduce, but i'll take a foreach loop.

I've tried this, but i get an empty array

 $result = array_reduce($source, function($a, $b)
 {
      if (($a->user_id === $b->user_id) && ($a->item_id === $b->item_id))
      {
           $a->quantity += $b->quantity; return $a;
      }
      else
      {
           return $b;
      }
 }, []);

or this, but i get an array with user_id as index and quantity as value as a overall total, not seperate by item_id:

$result = array_reduce($source, function($a, $b)
 {
      (($a[$b["user_id"]] = $a[$b["user_id"]]) && ($a[$b["item_id"]] = $a[$b["item_id"]])) ?? 0;
      $a[$b["user_id"]] += $b["quantity"];
      return $a;
 }, []);

I'm starting to try with a foreach loop with if (isset[...]) but i feel like it will return true whatever the index of the $result[] array.

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
Bo. Ga.
  • 138
  • 1
  • 4
  • 12

1 Answers1

1

You can use array_reduce function in next way:

$res = array_reduce (
    $source,
    function($res, $el) {
        if (isset($res[$el['user_id'].$el['item_id']])) {
            // if user_id with item_id exist 
            // increase quantity by $el['quantity']
            $res[$el['user_id'].$el['item_id']]['quantity'] += $el['quantity']; 
        } else {
            // create record with unique key for each user_id & item_id
            // using concatenation  
            $res[$el['user_id'].$el['item_id']] = $el;
        }
        
        return $res;
    },
    [] // initial result - empty array
);
// output result values without keys
print_r(array_values($res));

PHP array_reduce online

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
  • Ok, so you are creating the `$result` array with custom index by concatenating `user_id` and `item_id`, then you get rid of this index with `array_values`. Nice, i'll give it a try – Bo. Ga. Dec 16 '21 at 11:39
  • You can look a working code sample online https://phpize.online/sql/mysql57/undefined/php/php8/4748164821ab0f62ba4e46f1b0b89554/ – Slava Rozhnev Dec 16 '21 at 11:42