2016-11-13 53 views
-3

如何訪問位於下方的[0] - > Array()並通過遍歷數組檢索[Spirited Away]的[title]?我將如何去遍歷這個JSON對象?

Array 
(
    [@attributes] => Array 
    (
     [version] => 2.0 
    ) 

[channel] => Array 
    (
     [title] => Site Title 
     [link] => site/ 
     [language] => en-us 
     [category] => All 
     [image] => Array 
      (
       [title] => Site 
       [url] => http://example.com 
       [link] => example.com 
      ) 

     [item] => Array 
      (
       [0] => Array 
        (
         [title] => Spirited Away 
         [pubDate] => date 
         [category] => Movies 
         [link] => linkhere 
         [enclosure] => Array 
          (
           [@attributes] => Array 
            (
             [url] => someurlhere 
             [length] => length 
             [type] => application/x-bittorrent 
            ) 

          ) 

我目前正在嘗試使用foreach循環迭代數組,但它不輸出任何內容。我會發布我的代碼,但這個問題的代碼部分已經太長了。我正在使用PHP,但迄今爲止我都沒有嘗試過。

此JSON代碼是使用json_encode從XML文檔生成的,然後使用json_decode解碼爲JSON對象。

+0

'回聲$陣列[ '通道'] [ '項目'] [0] [ '標題'];' –

+1

'的foreach($陣列[ 'channel'] ['item'] as $ x){echo $ x ['title'];)' – 2016-11-13 02:47:41

+0

將JSON字符串添加到您的問題總是一個好主意。然後人們可以真正地建立他們答案的測試 – RiggsFolly

回答

1

評論回答爲正確。

遍歷這個特定的陣列結構其:

foreach ($array['channel']['item'] as $x){ 
    echo $x['title']; 
}