Jump to content

Recommended Posts

Hi guys,

Any help on this one would be MUCH appreciated please!

I have a multi-dimensional array.  Example of one element is as follows:

array(6) { [0]=> array(4) { 
      ["id"]=> int(123456) 
      ["total_price"]=> string(5) "15.48" 
      ["name"]=> string(5) "#1559" 
      ["line_items"]=> array(1) 
                 {[0]=> array(24) 
                     {["id"]=> int(1482631512137) 
                      ["variant_id"]=> int(12768293191753) 
                      ["title"]=> string(20) "abc" 
                      ["quantity"]=> int(1) 
                      ["price"]=> string(5) "12.49" 
                      ["sku"]=> string(3) "MM1" ["variant_title"]=> string(0) "" 
                      ["vendor"]=> string(15) "abc" 
                      ["fulfillment_service"]=> string(6) "manual" 
                      ["product_id"]=> int(1486884962377) 
                      ["requires_shipping"]=> bool(true) 
                      ["taxable"]=> bool(true) 
                      ["gift_card"]=> bool(false) 
                      ["name"]=> string(20) "xyz" 
                      ["variant_inventory_management"]=> string(7) "xxx" 
                      ["properties"]=> array(0) 
                          {
                          }  
                      ["product_exists"]=> bool(true) 
                      ["fulfillable_quantity"]=> int(1) 
                      ["grams"]=> int(222) 
                      ["total_discount"]=> string(4) "0.00" 
                      ["fulfillment_status"]=> NULL 
                      ["discount_allocations"]=> array(0) 
                          {
                          }
                      ["admin_graphql_api_id"]=> string(36) "vvv" 
                      ["tax_lines"]=> array(1) 
                          { [0]=> array(3) 
                              { 
                              ["title"]=> string(3) "VAT" 
                              ["price"]=> string(4) "2.08" 
                              ["rate"]=> float(0.2)
                              } 
                          } 
                 } 
        } 
    }

 

I need to step through every element in the array and add up all of the "quantity" fields.  

How would I go about that please?

Thanks,

Rob.

Link to comment
https://forums.phpfreaks.com/topic/307705-multidimensional-array-help/
Share on other sites

try

$total = 0;
foreach ($array as $order) {
    foreach ($order as $item) {
        foreach ($item['line_items'] as $li) {
            $total += $li['quantity'];
        }
    }
}

If that doesn't work, post the output from var_export() instead of var_dump() and I'll try again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.