How can I parse a JSON file with PHP? -
i tried parse json file using php. stuck now.
this content of json file:
{ "john": { "status":"wait" }, "jennifer": { "status":"active" }, "james": { "status":"active", "age":56, "count":10, "progress":0.0029857, "bad":0 } }
and have tried far:
<?php $string = file_get_contents("/home/michael/test.json"); $json_a = json_decode($string, true); echo $json_a['john'][status]; echo $json_a['jennifer'][status];
but because don't know names (like 'john'
, 'jennifer'
) , available keys , values (like 'age'
, 'count'
) beforehand, think need create foreach loop.
i appreciate example this.
to iterate on multidimensional array, can use recursivearrayiterator
$jsoniterator = new recursiveiteratoriterator( new recursivearrayiterator(json_decode($json, true)), recursiveiteratoriterator::self_first); foreach ($jsoniterator $key => $val) { if(is_array($val)) { echo "$key:\n"; } else { echo "$key => $val\n"; } }
output:
john: status => wait jennifer: status => active james: status => active age => 56 count => 10 progress => 0.0029857 bad => 0
Comments
Post a Comment