the5thace Posted July 9, 2013 Share Posted July 9, 2013 Sample query: http://www.faroo.com/api?q=elephants&start=1&length=10&l=en&src=web&f=json&jsoncallback=mycallback I do not quite understand the parameters I need to use for a foreach statement to work on this query. Previously I have managed the foreach to loop through Blekko results sets with foreach ($js->RESULT as $item) { if(@$item->snippet || @$item->url_title) {$Blekko[$i] = "<a href=\"{$item->url}\"> {$item->url_title}</a>blekko <p>$item->snippet}</p>"; but I'm not sure how it actually works. Bing query : https://api.datamarket.azure.com/Bing/Search/Web?$format=json&Query=%27bing%27 Link to comment https://forums.phpfreaks.com/topic/279988-how-to-use-a-foreach-statement-to-loop-through-json-search-engine-results/ Share on other sites More sharing options...
requinix Posted July 9, 2013 Share Posted July 9, 2013 Whatever variable you're json_decode()ing to, $obj = json_decode(/* whatever */); $obj->results is the array of results $obj->results[0] is the first one in that array $obj->results[0]->title is the first title Link to comment https://forums.phpfreaks.com/topic/279988-how-to-use-a-foreach-statement-to-loop-through-json-search-engine-results/#findComment-1440015 Share on other sites More sharing options...
the5thace Posted July 9, 2013 Author Share Posted July 9, 2013 I understand that but how do you know which variable offers the array of results? My current foreach does not work for the faroo query. Giving me an error on the foreach line. foreach ($js->results as $item) { $Faroo[$i] = "<a href=\"{$item->url}\"> {$item->title}</a>faroo <p>$item->kwic}</p>"; $i++; Link to comment https://forums.phpfreaks.com/topic/279988-how-to-use-a-foreach-statement-to-loop-through-json-search-engine-results/#findComment-1440016 Share on other sites More sharing options...
the5thace Posted July 9, 2013 Author Share Posted July 9, 2013 Wrong query given. I meant to give Blekko query: http://blekko.com/?q=dogs+/json+/ps=100&auth= Link to comment https://forums.phpfreaks.com/topic/279988-how-to-use-a-foreach-statement-to-loop-through-json-search-engine-results/#findComment-1440018 Share on other sites More sharing options...
requinix Posted July 9, 2013 Share Posted July 9, 2013 And that's a different JSON. Example: $js->RESULT $js->RESULT[0] $js->RESULT[0]->url_title Link to comment https://forums.phpfreaks.com/topic/279988-how-to-use-a-foreach-statement-to-loop-through-json-search-engine-results/#findComment-1440080 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.