2016-11-13 39 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']; 
}