php - Get a specific key from first level in array only -
im trying value specific key in array. however, key(name) exists @ 2 levels, , not have control of.
i need check array value of key 'totalweight' in multidimensional array.
im receiving array , doing check this....
$json = file_get_contents('php://input'); $body = json_decode($json, true); if($body['eventname'] == 'shippingrates.fetch') { $shippingweight = $body['content']['totalweight']; // other logic code here... }
the rest of code works fine, problem 'totalweight' exists @ 2 levels, , need first level (which total weight of cart, not individual items in cart...)
the array receive looks this:
{ "eventname": "shippingrates.fetch", "mode": "live", "createdon": "2014-11-11t23:47:00.6132556z", "content": { "items": [ { "totalweight": 1000, "customfields": [ { "name": "gift", "operation": null, "type": "checkbox", "options": "true|false", "required": false, "value": "false" } ], "unitprice": 180 }, { "totalweight": 1200, "customfields": [ { "name": "gift", "operation": null, "type": "checkbox", "options": "true|false", "required": false, "value": "false" } ], "unitprice": 200 } ], "totalweight": 9600, "customfields": [ { "name": "i have read , accept conditions of sale", "operation": null, "type": "checkbox", "options": "true|false", "required": true, "value": "true" } ], "itemscount": 5, "metadata": null } }
this how i'm checking....
if($shippingweight > '3000') { $rate = $shippingrateshigh; } elseif($shippingweight > '5000') { $rate = $shippingratesmax; } else { // default $rate = $shippingratesstd; }
when have multiple items in cart, bases 'totalweight' on 1 of entries inside ['items'] array, if 1 product exists check works...
how can 'totalweight' inside 'content' without travelling further array inside 'items'.
any appreciated!
well start json data receiving object i.e. wrapped in {}
, not array wrapped in []
why force array.
if use idea, think easier visualize.
$json = file_get_contents('php://input'); $body = json_decode($json); echo 'the total weight of shipments ' . $body->content->totalweight;
if want individual item weights or example check item weights equal total weight,
$sumofitemrates = 0; foreach( $body->content->items $item) { $sumofitemrates += $item->totalweight; } echo 'sum of individual items weights ' . $sumofitemrates;
i think objects easier , cleaner code arrays.
i tested code, here full test run using php cli:
<?php $json = '{ "eventname": "shippingrates.fetch", "mode": "live", "createdon": "2014-11-11t23:47:00.6132556z", "content": { "items": [ { "totalweight": 1000, "customfields": [ { "name": "gift", "operation": null, "type": "checkbox", "options": "true|false", "required": false, "value": "false" } ], "unitprice": 180 }, { "totalweight": 1200, "customfields": [ { "name": "gift", "operation": null, "type": "checkbox", "options": "true|false", "required": false, "value": "false" } ], "unitprice": 200 } ], "totalweight": 9600, "customfields": [ { "name": "i have read , accept conditions of sale", "operation": null, "type": "checkbox", "options": "true|false", "required": true, "value": "true" } ], "itemscount": 5, "metadata": null } }'; $body = json_decode($json); echo 'the total weight of shipments ' . $body->content->totalweight . php_eol; $sumofitemrates = 0; foreach( $body->content->items $item) { $sumofitemrates += $item->totalweight; } echo 'sum of individual items weights ' . $sumofitemrates;
and output
the total weight of shipments 9600 sum of individual items weights 2200
which believe asking do.
Comments
Post a Comment