2013-09-23 18:04

你能做一个$ json = file_get_contents($ url); 在已使用$ json = file_get_contents($ url);的foreach语句中? [关闭]

$url = ""
$json = file_get_contents($url);
$data = json_decode($json);

foreach($data as $mydata) {

    $id = $mydata->id;

        $url2 = "$id";
        $json2 = file_get_contents($url2);
        $data2 = json_decode($json2);
        var_dump($data2); // seems to always be null?? :(

            foreach($data2 as $mydata2) {


the error I get is Warning: Invalid argument supplied for foreach() which is because data2 is not an array....

Guessing I can't do file_get_contents() stacked like I am. Is there a way around this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doormen2014 doormen2014 8年前

    URL isn't returning valid JSON.

    点赞 评论 复制链接分享
  • doushou5761 doushou5761 8年前

    First check, is your url return a JSON? If yes then you can use following instead

    $data = json_decode($json, TRUE);

    The TRUE returns an array instead of an object.

    点赞 评论 复制链接分享