Jump to content

How to use a foreach statement to loop through json search engine results?


Recommended Posts

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 by the5thace

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

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++;
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.