Traverse a complex array of nested arrays using PHP -



Traverse a complex array of nested arrays using PHP -

i need new idea's, have tried different approach's using foreach statement along iteratorts in php5. parsing response ups multi bundle rate response.

the api returns of service offerings, ground, next day air etc. want focus in key service array holds 'code' value. 1 time find's right service value, when want pull array ratedpackages traverse different services , costs of each package.

i have tried next code no avail, rather see different approaches, not master when comes parsing arrays. is improve send array different construction such json etc. have no idea.

solution expected when 'service array fellow member code - 01 when need pull rated packages array can produce shipping label.

if has improve aproach open , suggestions, if not clear inquire me question , respond best of ability.

thank in advance

anyway here code:

try { $object = new arrayiterator( $arr ); $object->rewind(); while( $object->valid()){ echo $object->key(). "-&gt; ". $object->current()."<br>"; if(is_array($object->current())) { foreach($object->current() $node => $value) { //echo "item: ".$item. " value: ".$value. "<br>"; if($node == 'service') { if(is_array($value)) { s($value->code); foreach($value $item => $v){ echo "item: ".$item. " value: ".$v. "<br>"; if($v == '01'){ echo "hit our mark!!!!!!!"; $rp = $node[0]['ratedpackage']; s("rp: ".$node); } } } } } } $object->next(); } } grab (exception $e) { echo $e->getmessage(); }

output code

0-> array null item: code value: 03 1-> array null item: code value: 12 2-> array null item: code value: 02 3-> array null item: code value: 13 4-> array null item: code value: 14 5-> array null item: code value: 01 nail our mark!!!!!!! warning: illegal string offset 'ratedpackage' in /applications/mamp/htdocs/pkdev/dev/portal/shipping.php on line 93 string (11) "rp: service" ![snapshot of array response][1] array parse array(1) [ 'ratingserviceselectionresponse' => array(2) [ 'response' => array(3) [ 'transactionreference' => array(1) [ 'customercontext' => string (12) "rocketshipit" ] 'responsestatuscode' => string (1) "1" 'responsestatusdescription' => string (7) "success" ] 'ratedshipment' => array(6) [ array(9) [ 'service' => array(1) [ 'code' => string (2) "03" ] 'ratedshipmentwarning' => string (56) "your invoice may vary displayed reference rates" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ 'code' => string (3) "lbs" ] 'weight' => string (4) "35.0" ] 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "37.12" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "37.12" ] 'guaranteeddaystodelivery' => string (0) "" 'scheduleddeliverytime' => string (0) "" 'ratedpackage' => array(3) [ array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "10.91" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "10.91" ] 'weight' => string (3) "5.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (3) "5.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "12.24" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "12.24" ] 'weight' => string (4) "10.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "10.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "13.97" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "13.97" ] 'weight' => string (4) "20.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "20.0" ] ] ] ] array(9) [ 'service' => array(1) [ 'code' => string (2) "12" ] 'ratedshipmentwarning' => string (56) "your invoice may vary displayed reference rates" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ 'code' => string (3) "lbs" ] 'weight' => string (4) "35.0" ] 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "59.57" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "59.57" ] 'guaranteeddaystodelivery' => string (1) "3" 'scheduleddeliverytime' => string (0) "" 'ratedpackage' => array(3) [ array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "15.29" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "15.29" ] 'weight' => string (3) "5.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (3) "5.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "18.59" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "18.59" ] 'weight' => string (4) "10.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "10.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "25.69" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "25.69" ] 'weight' => string (4) "20.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "20.0" ] ] ] ] array(9) [ 'service' => array(1) [ 'code' => string (2) "02" ] 'ratedshipmentwarning' => string (56) "your invoice may vary displayed reference rates" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ 'code' => string (3) "lbs" ] 'weight' => string (4) "35.0" ] 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "77.78" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "77.78" ] 'guaranteeddaystodelivery' => string (1) "2" 'scheduleddeliverytime' => string (0) "" 'ratedpackage' => array(3) [ array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "19.86" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "19.86" ] 'weight' => string (3) "5.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (3) "5.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "23.87" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "23.87" ] 'weight' => string (4) "10.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "10.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "34.05" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "34.05" ] 'weight' => string (4) "20.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "20.0" ] ] ] ] array(9) [ 'service' => array(1) [ 'code' => string (2) "13" ] 'ratedshipmentwarning' => string (56) "your invoice may vary displayed reference rates" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ 'code' => string (3) "lbs" ] 'weight' => string (4) "35.0" ] 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (6) "121.50" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (6) "121.50" ] 'guaranteeddaystodelivery' => string (1) "1" 'scheduleddeliverytime' => string (0) "" 'ratedpackage' => array(3) [ array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "32.29" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "32.29" ] 'weight' => string (3) "5.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (3) "5.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "37.95" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "37.95" ] 'weight' => string (4) "10.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "10.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "51.26" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "51.26" ] 'weight' => string (4) "20.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "20.0" ] ] ] ] array(9) [ 'service' => array(1) [ 'code' => string (2) "01" ] 'ratedshipmentwarning' => string (56) "your invoice may vary displayed reference rates" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ 'code' => string (3) "lbs" ] 'weight' => string (4) "35.0" ] 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (6) "139.05" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (6) "139.05" ] 'guaranteeddaystodelivery' => string (1) "1" 'scheduleddeliverytime' => string (10) "10:30 a.m." 'ratedpackage' => array(3) [ array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "37.02" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "37.02" ] 'weight' => string (3) "5.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (3) "5.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "43.18" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "43.18" ] 'weight' => string (4) "10.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "10.0" ] ] array(5) [ 'transportationcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "58.85" ] 'serviceoptionscharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (4) "0.00" ] 'totalcharges' => array(2) [ 'currencycode' => string (3) "usd" 'monetaryvalue' => string (5) "58.85" ] 'weight' => string (4) "20.0" 'billingweight' => array(2) [ 'unitofmeasurement' => array(1) [ *depth great* ] 'weight' => string (4) "20.0" ] ] ] ] ] ] ]

php arrays

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -