开发者

Trouble looping through an array created from a foursquare JSON feed

开发者 https://www.devze.com 2023-02-04 16:28 出处:网络
I\'m working on a side project and at its core, I need to get a foursquare json feed into an array that i can loop through. My code is below and r开发者_如何学JAVAesults in the following error:

I'm working on a side project and at its core, I need to get a foursquare json feed into an array that i can loop through. My code is below and r开发者_如何学JAVAesults in the following error:

Warning: Invalid argument supplied for foreach() in /homepages/7/d346835943/htdocs/dealrub/results.php on line 56

Here is an example of the json feed that i am correctly acquiring:

$jsonurl = "http://api.foursquare.com/v2/venues/search?ll=".$lat.",".$lon."&limit=100";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_encode($json, true);

foreach ( $json_output->response->groups[0]->items as $items )
{
     echo "{$items->name}\n";
}

Any help as to what i'm doing wrong would be greatly appreciated. I left the jsonurl without my api key, but it is successfully returning the json results.


  1. You have to use json_decode.
  2. Check whether $json_ouput is not empty.
  3. You are passing true as second argument to json_decode (assuming you have it right) which means that it returns an associative array.

    Either omit that:

    $json_output = json_decode($json);
    

    or access items as array:

    foreach ( $json_output['response']['groups'][0]['items'] as $items )
    


You're using json_encode on a string that is already in json. Try json_decode instead ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消