-1

我有一个 json 文件(相当大,请注意,同一文件的 xml 版本超过 19000 行),如下所示:

object(stdClass)#1 (1) {
    ["result"]=> object(stdClass)#2 (13) {
        ["items"]=> array(1628) {
            [0]=> object(stdClass)#27 (18) {
                ["image_url"]=> string(95) "http://media.steampowered.com/apps/440/icons/c_bat.d037d6a40ec30ab4aa009387d476dca889b6f7dc.png"
            }
            [1]=> object(stdClass)#29 (18) {
                ["image_url"]=> string(98) "http://media.steampowered.com/apps/440/icons/w_bottle.859ddb315a2748f04bcc211aa7a04f2c926e6169.png"
            }
        }
    }
}

我想获取“项目”数组中每个对象的图像 url,我尝试这样做:

<?PHP

$api = "http://api.steampowered.com/IEconItems_440/GetSchema/v0001/?key=yoink&language=en_US&format=json";

$json = file_get_contents($api);

$schema = json_decode($json);

print count($schema->result->items);

foreach($schema->results->items as $item) {
    print "{$item->image_url}";
} 

?>

但是当我检查它打印出来的页面时,我得到了这个:

1628 Warning: Invalid argument supplied for foreach() in - on line 13

现在我知道这个数组绝对不是空的,因为 count 返回 1628,而且它也不为空,因为我之前有一个 var_dump,如上所示。谁能帮我看看我哪里出错了?

编辑:我需要学习如何阅读。请投票结束这个问题!

4

2 回答 2

1

Typo, change:

foreach($schema->results->items as $item) {
                       ^-- here

to:

foreach($schema->result->items as $item) {
于 2013-06-19T18:02:58.587 回答
0

Typo. It's not results, it's result.

foreach($schema->result->items as $item) {
    print "{$item->image_url}";
} 
于 2013-06-19T18:02:47.640 回答