Reference PHP Sub-Arrays by Key -
i playing php 5.4 process info returned http api. info comes in xml format convert array using following:
$xml = simplexml_load_string($resp); $json = json_encode($xml); $arr = json_decode($json, true); this gives me info in array form (if there improve way this, please tell me!). result next sample array:
array ( '@attributes' => array ( 'status' => 'success', 'code' => '19', ), 'result' => array ( '@attributes' => array ( 'total-count' => '1', 'count' => '1', ), 'user' => array ( 'entry' => array ( 0 => array ( '@attributes' => array ( 'name' => 'chris', ), 'phash' => 's98djf384jr0oq8jf8j3948jfw', ), 1 => array ( '@attributes' => array ( 'name' => 'test', ), 'phash' => '9a8sdfu9n2308ja8fj34ojr9a0', ), ), ), ), ) what trying figure out how reference various elements of array. have tried referencing index echo $arr[0][0] doesn't homecoming , can't find out how reference sub-arrays key.
this documented in php manual.
http://www.php.net/manual/en/language.types.array.php
arrays in php key/value pairs. when key not specified php utilize numeric indexes.
you can access values $arr['@attributes']['status']
to check if key exists can utilize isset($arr['@attributes']) or array_key_exists('@attributes',$arr).
to enumerate
foreach($arr $key=>$value) { .... } http://ca1.php.net/manual/en/control-structures.foreach.php
php arrays xml json
No comments:
Post a Comment