如何遍历此数组并调用value / s?

时间:2015-01-18 11:15:27

标签: php arrays xml

我首先有一个XML文件,我将其转换为可在下面看到的数组。但我遇到的问题是循环遍历数组并调用值。例如,我如何通过动漫来获取动画名称'攻击泰坦'或者开始时间,上午9点30分'等等?我怎样才能做到这一点?谢谢。

这是数组:

Array (
 [anime] => Array (
  [0] => Array (
   [id] => 1
    [content] => Array (
     [action_adventure_channel] => Array (
      [0] => Array (
       [name] => Array (
        [0] => Attach on Titan
        )
        [start_time] => Array (
         [0] => 09.30am
        )
        [end_time] => Array (
         [0] => 10.30am
        )
       )
      )
     )
    )
    [1] => Array (
     [id] => 2
     [content] => Array (
      [crime_drama_channel] => Array (
       [0] => Array (
        [name] => Array (
         [0] => Death Note
        )
        [start_time] => Array (
         [0] => 12.00pm
        )
        [end_time] => Array (
         [0] => 1.00pm
        )
       )
      )
     )
    )
   )
  )

编辑:这是我转换为上述数组的XML版本:

<?xml version="1.0" encoding="UTF-8"?>
<anime_data>
    <anime id="1">
    <action_adventure_channel>
        <name>Attach on Titan</name>
        <start_time>09.30am</start_time>
        <end_time>10.30am</end_time>
    </action_adventure_channel>
    </anime>
    <anime id="2">
    <crime_drama_channel>
        <name>Death Note</name>
        <start_time>12.00pm</start_time>
        <end_time>1.00pm</end_time>
    </crime_drama_channel>
    </anime>
</anime_data>

1 个答案:

答案 0 :(得分:1)

您可以从一开始就使用SimpleXML。然后使用foreach进行遍历。

$xml = simplexml_load_file('path/to/xmlfile.xml');
foreach($xml as $anime) {
    $id = $anime->attributes()->id;
    foreach($anime as $info) {
        $name = $info->name;
        $start_time = $info->start_time;
        $end_time = $info->end_time;
    }
}

Sample Output