2013-06-19 36 views
-1

I have a json file(相當大,請記住,同一個文件的xml版本超過19000線長)是這樣的:非空,非空數組不斷拋出'警告:無效參數爲foreach()提供'當我使用foreach()

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,也不是null,因爲前面已經看到了var_dump。任何人都可以幫助我瞭解我出錯的地方嗎?

編輯:我需要學習如何閱讀。請投票結束這個問題!

+0

是否每個條目總是有一個IMAGE_URL?如果有人失蹤了,我懷疑你會得到那個錯誤。 – andrewsi

回答

0

Typo。這不是results,它是result

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

錯字,變化:

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

到:

foreach($schema->result->items as $item) { 
相關問題