Rob2018 Posted September 20, 2018 Share Posted September 20, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307705-multidimensional-array-help/ Share on other sites More sharing options...
Barand Posted September 20, 2018 Share Posted September 20, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307705-multidimensional-array-help/#findComment-1560964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.