Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is foreach over a results object not working?

I realize there are quite a few questions of this nature, but I have not been able to solve my issue using the other posts, so I was hoping someone here may be able to help me out.

I have an object I'm getting back from Yahoo local search API. I have passed the results into json_decode() and saved the results to $yahoo_json_decoded. I can get data from the results using a for loop and doing the following:

echo 'Name: ' . $yahoo_json_decoded->ResultSet->Result[$i]->Title . '<br />' ;

But I can't seem to be able to make the foreach work:

foreach($yahoo_json_decoded->ResultSet as $res=>$variable)
{
    $listingID = $yahoo_json_decoded->ResultSet[$res]->id ;
    echo $listingID;
}

I could just loop the data and move on, but I really want to understand why the foreach isn't working.

Thanks (show mercy)

Kirk

like image 864
Kirk B Avatar asked Dec 09 '22 16:12

Kirk B


1 Answers

Based on $yahoo_json_decoded->ResultSet->Result[$i]->Title which you say works:

foreach($yahoo_json_decoded->ResultSet->Result as $index => $result)
{
   $listingID = $result->id ;
    echo $listingID;
}
like image 82
MichaelRushton Avatar answered Dec 11 '22 10:12

MichaelRushton