Count specific elements of an array in php -
i count specific elements ('host_name') of array.
my function decoding json file , returning array.
file.json
{ "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" } ] } my php page decode json file :
functions.php
function test($clustername) { $content = file_get_contents($clustername.".json"); $content = utf8_encode($content); $result = json_decode($content, true); return $result; } and page i'd display number of host names :
page.php
$result = test("mycluster"); echo "<p>".count($result['hosts']['host_name'])."</p>"; but have undefined index 'hosts' when this. tried things, can tell array there (print_r shows it), can't count elements of it.
thanks
you have count elemets of "items".
echo "<p>".count($result['items'])."</p>";
Comments
Post a Comment