循环读取 PHP 数组输出
Posted
技术标签:
【中文标题】循环读取 PHP 数组输出【英文标题】:Reading PHP array output in a loop 【发布时间】: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
没有显示任何内容
【问题讨论】:
将 simleXMLObject 转换为数组 -> ***.com/questions/6167279/… 【参考方案1】:foreach($xml->GetAllDomains->DomainDetail as $domainDetail)
echo $domainDetail->DomainName, "\n";
echo $domainDetail->DomainNameID, "\n";
echo $domainDetail->'expiration-date', "\n";
// etc..
【讨论】:
以上是关于循环读取 PHP 数组输出的主要内容,如果未能解决你的问题,请参考以下文章