PHP:从simplexmloelement对象中回显一个值

时间:2012-10-31 16:41:52

标签: php arrays simplexml

这是我的代码:

$xml2  = (array) simplexml_load_string($xml_file);

使用print_r这是我的数组:

 Array
(
  [program] => Array
        (
        [0] => SimpleXMLElement Object
        (
            [date] => asgasg sgasgasg
            [start_time] => asdg asg
            [leadtext] => as asdgsagsdgasgasgd
            [name] => as gsadgasg
            [bline] => sag asdg
            [synopsis] => asg asga sdg
            [url] => asg sdgasgasg
        )

        [1] => SimpleXMLElement Object
        (
            [date] => sgasgasg1
            [start_time] => asg1
            [leadtext] => as1
            [name] => gsadgasg1
            [bline] => asdg1
            [synopsis] => sdg1
            [url] =>sdgasgasg1
        )

        )
)

如何从第二个SimpleXMLElement(上例中的 sgasgasg1 )获取[date]的内容?

2 个答案:

答案 0 :(得分:2)

使用

foreach($xml2['program'] as $key=>$value) {

   echo $value->date;

}

正如你在评论中所说的那样

echo $xml2['program'][1]->date;

答案 1 :(得分:2)

你可以这样做:

echo $xml2['program'][1]->date;