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

$url = "www.test.com"
$json = file_get_contents($url);
$data = json_decode($json);

foreach($data as $mydata) {

    $id = $mydata->id;

        $url2 = "www.test.com/$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?

URL isn't returning valid JSON.

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.