【问题标题】:Reading PHP array output in a loop循环读取 PHP 数组输出
【发布时间】:2015-08-07 04:40:48
【问题描述】:

我在读取 XML 文件时有这个输出。

http://pastebin.com/NfNHBaHt

SimpleXMLElement Object
(
    [GetAllDomains] => SimpleXMLElement Object
        (
            [DomainDetail] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [DomainName] => domain1.com
                            [DomainNameID] => 153033597
                            [expiration-date] => 3/1/2017 2:23:56 PM
                            [lockstatus] => Locked
                            [AutoRenew] => Yes
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [DomainName] => domain2.com
                            [DomainNameID] => 153043233
                            [expiration-date] => 5/25/2017 5:45:00 PM
                            [lockstatus] => Not Locked
                            [AutoRenew] => Yes
                        )

                )

            [domaincount] => 2
            [UserRequestStatus] => DomainBox
        )

    [Command] => GETALLDOMAINS
    [APIType] => API
    [Language] => eng
    [ErrCount] => 0
    [ResponseCount] => 0
    [MinPeriod] => SimpleXMLElement Object
        (
        )

    [MaxPeriod] => 10
    [Server] => SJL0VWRESELL_T1
    [Site] => eNom
    [IsLockable] => SimpleXMLElement Object
        (
        )

    [IsRealTimeTLD] => SimpleXMLElement Object
        (
        )

    [TimeDifference] => +0.00
    [ExecTime] => 0.094
    [Done] => true
    [RequestDateTime] => 5/25/2015 9:47:21 AM
    [debug] => SimpleXMLElement Object
        (
        )

)

上面的输出是使用 PHP 中的 print_r 函数显示的

如何在 PHP 中使用 foreach 循环遍历 DomainDetail 的数组

我尝试使用print_r($xml->DomainDetail);print_r($xml["DomainDetail"]);

但使用此print_r 没有显示任何内容

【问题讨论】:

标签: php simplexml


【解决方案1】:
foreach($xml->GetAllDomains->DomainDetail as $domainDetail) {
    echo $domainDetail->DomainName, "\n";
    echo $domainDetail->DomainNameID, "\n";
    echo $domainDetail->{'expiration-date'}, "\n";
    // etc..
}

【讨论】:

    猜你喜欢
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2010-12-13
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多