the5thace Posted July 9, 2013 Share Posted July 9, 2013 (edited) 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 Edited July 9, 2013 by the5thace Quote 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 Quote 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++; Quote 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 (edited) Wrong query given. I meant to give Blekko query: http://blekko.com/?q=dogs+/json+/ps=100&auth= Edited July 9, 2013 by the5thace Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.