0

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

3 Answers 3

2

Try:

foreach ($json_output->results as $object){
    print "{$object->title}\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. The problem was in your array name, which in JSON was defined as "results" not "dataObjects"
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.