1 year ago

How to Sum Two Different Arrays

Posted 1 year ago by vincej

I have two different arrays, no big deal, except that one is an array of objects the other a straight array.

So here is one:

$allocation = {array} [4]
 0 = {stdClass} [2]
  inv_product_id = 123
  allocated = "4"
 1 = {stdClass} [2]
  inv_product_id = 789
  allocated = "8"

etc etc

Here is the other:

$orders = {array} [6]
 123 = "131"
 789 = "12"

etc etc

So, the objective is sum $allocation values ie allocated together with the $orders value. So, in this example, for product code 123, the sum total would be 135.

I have been trying to convert $allocation into the same format as $orders without success. My belief has been that once they are in the same format, then summing them should not be too bad. But I have not gotten that far yet. Most of my efforts have revolved around using foreach loops or for loops and array_push

I have looked carefully at the Laravel helpers but nothing has worked for me yet.

Any ideas would be really welcome, as I have been beating my head over this.

Many thanks !

Please sign in or create an account to participate in this conversation.