Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TuffRivers's avatar

Multiplying Values of seperate Arrays and combining them

Hi All,

I have json that contains multiple products from an order. I convert this json into an array, then i am trying to run a foreach over each product to:

  1. extract the key and array (quantity and weight)
  2. Multiply the key and array together
  3. Add all the sums together to get a total (across multiple arrays)

But i am having a lot of trouble doing so.

Here is my code, any idea how i can achieve this?


// my json
$products = '[
    {
        "id": 61,
        "order_id": 40,
        "product_id": 10,
        "order_address_id": 43,
        "name": "Large Bag ",
        "sku": "111222",
        "type": "physical",
        "base_price": "0.0000",
        "price_ex_tax": "0.0000",
        "price_inc_tax": "0.0000",
        "price_tax": "0.0000",
        "base_total": "0.0000",
        "total_ex_tax": "0.0000",
        "total_inc_tax": "0.0000",
        "total_tax": "0.0000",
        "weight": "15.0000",
        "quantity": 1,
        "base_cost_price": "0.0000",
        "cost_price_inc_tax": "0.0000",
        "cost_price_ex_tax": "0.0000",
        "cost_price_tax": "0.0000",
        "is_refunded": false,
        "quantity_refunded": 0,
        "refund_amount": "0.0000",
        "return_id": 0,
        "wrapping_name": "",
        "base_wrapping_cost": "0.0000",
        "wrapping_cost_ex_tax": "0.0000",
        "wrapping_cost_inc_tax": "0.0000",
        "wrapping_cost_tax": "0.0000",
        "wrapping_message": "",
        "quantity_shipped": 1,
        "event_name": null,
        "event_date": "",
        "fixed_shipping_cost": "0.0000",
        "ebay_item_id": "",
        "ebay_transaction_id": "",
        "option_set_id": null,
        "parent_order_product_id": null,
        "is_bundled_product": false,
        "bin_picking_number": "",
        "external_id": null,
        "fulfillment_source": "",
        "applied_discounts": [],
        "product_options": [],
        "configurable_fields": []
    },
    {
        "id": 62,
        "order_id": 40,
        "product_id": 17,
        "order_address_id": 41,
        "name": "Rod",
        "sku": "11112223,
        "type": "physical",
        "base_price": "0.0000",
        "price_ex_tax": "0.0000",
        "price_inc_tax": "0.0000",
        "price_tax": "0.0000",
        "base_total": "0.0000",
        "total_ex_tax": "0.0000",
        "total_inc_tax": "0.0000",
        "total_tax": "0.0000",
        "weight": "1.0000",
        "quantity": 2,
        "base_cost_price": "0.0000",
        "cost_price_inc_tax": "0.0000",
        "cost_price_ex_tax": "0.0000",
        "cost_price_tax": "0.0000",
        "is_refunded": false,
        "quantity_refunded": 0,
        "refund_amount": "0.0000",
        "return_id": 0,
        "wrapping_name": "",
        "base_wrapping_cost": "0.0000",
        "wrapping_cost_ex_tax": "0.0000",
        "wrapping_cost_inc_tax": "0.0000",
        "wrapping_cost_tax": "0.0000",
        "wrapping_message": "",
        "quantity_shipped": 2,
        "event_name": null,
        "event_date": "",
        "fixed_shipping_cost": "0.0000",
        "ebay_item_id": "",
        "ebay_transaction_id": "",
        "option_set_id": null,
        "parent_order_product_id": null,
        "is_bundled_product": false,
        "bin_picking_number": "",
        "external_id": null,
        "fulfillment_source": "",
        "applied_discounts": [],
        "product_options": [],
        "configurable_fields": []
    }
]';

//convert json to array
$products = json_decode($products, true);

foreach($products as $product){

    $weight = $product["weight"] ;
    $quantity = $product["quantity"];
    
//this doesnt work, how cani  perform the operation and add the value to an array?
    $totalWeight = array($product["weight"] * $product["quantity"]);

   $total = array_sum($totalWeight);
    
}

0 likes
2 replies
MikeRees's avatar
MikeRees
Best Answer
Level 1

You are creating your array within your foreach, and constantly overwriting it. Here's how I would fix it:

$totalWeight = [];
foreach($products as $product){

    $weight = $product["weight"] ;
    $quantity = $product["quantity"];
    
   $totalWeight[]  = array($product["weight"] * $product["quantity"]);
   
}
$total = array_sum($totalWeight);

That being said, you're needlessly using an array. Could you not just set total to 0 before the loop and then do this in the loop?

$total += $product["weight"] * $product["quantity"];
1 like
TuffRivers's avatar

@MikeRees

wow that is so simple.

I set total to 0 and used your += and it worked perfectly. I was definitely over thinking this.

Thank you so much

Please or to participate in this conversation.