Trouble looping through an array created from a foursquare JSON feed

▼魔方 西西 提交于 2019-12-12 01:35:40

问题


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 results 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:


  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 )
    



回答2:


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



来源:https://stackoverflow.com/questions/4676546/trouble-looping-through-an-array-created-from-a-foursquare-json-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!