问题描述:

Hi I'm trying to loop through a json array of objects but I literally get nothing back, here is my attempt.

 $jsonurl = 'http://eol.org/api/search/1.0.json?q='."$searchvar".'&page=1&exact=false& filter_by_taxon_concept_id=&filter_by_hierarchy_entry_id=&filter_by_string=&cache_ttl=';

$json = file_get_contents($jsonurl,0,null,null);

$json_output = json_decode($json);

foreach ($json_output->dataObjects as $objects){

print "{$objects->title}\n";

}

here is the structure of the actual json array.

array

0 object

id 19076

title Vulpes

link http://eol.org/19076?action=overview&controller=taxa

content Arctic foxes; kit foxes; red foxes; red fox

网友答案:

Try:

foreach ($json_output->results as $object){
    print "{$object->title}\n";
}
网友答案:

one more approach that you can try is to decode the json in to array instead of objects.

$json_output = json_decode($json,true);

By passing a varaible as true in the json_decode() function you will get the json_output as an array which can be easily traversed.

Else

you can loop through

foreach ($json_output->results as $object){
    echo "{$object->title}\n";
}

Please refer to the json_decode() function php manual guide at http://php.net/manual/en/function.json-decode.php

网友答案:

i hope this could be helpful to you

try to type cast with object

foreach ($json_output->dataObjects as $objects){

let me know if i can help you more

相关阅读:
Top