arrays - Parse a multi level json in php -
i have json file , "host_name" values items. below, php code, tried things, using true decode json in array or without , use foreach()
, can't it.
{ "href": "https://webservice:8080", "items": [ { "hosts": { "cluster_name": "cluster1", "host_name": "server1" }, "href": "https://server1:8080" }, { "hosts": { "cluster_name": "cluster1", "host_name": "server2" }, "href": "https://server2:8080" }, { "hosts": { "cluster_name": "cluster1", "host_name": "server3" }, "href": "https://server3:8080" } ] }
here php code. simple possible. goal use last values of json ('host_name') display of them.
<?php $content = file_get_contents("test.json"); $content = utf8_encode($content); $result = json_decode($content, true); var_dump($result); echo "<br><br><br>"; #echo $result["items"]["hosts"][0]["host_name"]; foreach($result $r) { echo $r['items']['hosts']['cluster_name'].'<br>'; } ?>
thanks.
you looping on wrong array, should be
foreach($result['items'] $item) { echo $item['hosts']['host_name']; }
Comments
Post a Comment