加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在PHP中循环使用SimpleXML对象

发布时间:2020-12-13 14:13:12 所属栏目:PHP教程 来源:网络整理
导读:我有一个simpleXml对象,并希望从对象中读取数据,我是 PHP的新手,并不知道如何做到这一点.对象细节如下. 我需要阅读[描述]和[小时].谢谢. SimpleXMLElement Object ( [@attributes] = Array ( [type] = array ) [time-entry] = Array ( [0] = SimpleXMLElemen
我有一个simpleXml对象,并希望从对象中读取数据,我是 PHP的新手,并不知道如何做到这一点.对象细节如下.

我需要阅读[描述]和[小时].谢谢.

SimpleXMLElement Object (
    [@attributes] => Array (
        [type] => array
    )
    [time-entry] => Array (
        [0] => SimpleXMLElement Object (
            [date] => 2010-01-26
            [description] => TCDM1 data management: sort & upload
            [hours] => 1.0
            [id] => 21753865
            [person-id] => 350501
            [project-id] => 4287373
            [todo-item-id] => SimpleXMLElement Object (
                [@attributes] => Array (
                    [type] => integer
                    [nil] => true
                )
            )
        )
        [1] => SimpleXMLElement Object (
            [date] => 2010-01-27
            [description] => PDCH1: HTML
            [hours] => 0.25
            [id] => 21782012
            [person-id] => 1828493
            [project-id] => 4249185
            [todo-item-id] => SimpleXMLElement Object (
                [@attributes] => Array (
                    [type] => integer
                    [nil] => true
                )
            )
)   )   )

请帮我.我尝试了很多东西,但没有正确的语法.

如果您可以给我们一个屏幕截图或给出行间距和缩进,那么上面的内容真的很难读,那会更好一点
如果你有一个对象

$xml是一个simplexml对象,你可以访问像

$xml->element.

它看起来像是时间输入的元素,在这种情况下你会像这样循环:

foreach( $xml->{'time-entry'} as $time_entry ) {
    echo $time_entry->description . "<br />n";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读