如何打印 SimpleXMLElement 对象的特定数组元素
Posted
技术标签:
【中文标题】如何打印 SimpleXMLElement 对象的特定数组元素【英文标题】:How to print specific array element of SimpleXMLElement object 【发布时间】:2020-09-27 10:34:02 【问题描述】:我想从任何数组的响应中获取结果。这是数组输出:
SimpleXMLElement Object
(
[GetCharitiesByKeywordResponse] => SimpleXMLElement Object
(
[GetCharitiesByKeywordResult] => SimpleXMLElement Object
(
[CharityList] => Array
(
[0] => SimpleXMLElement Object
(
[RegisteredCharityNumber] => 1175358
[SubsidiaryNumber] => 0
[CharityName] => LYDIARD PARK HERITAGE TRUST
[MainCharityName] => LYDIARD PARK HERITAGE TRUST
[RegistrationStatus] => Registered
[PublicEmailAddress] => info@lydiardtrust.org.uk
[MainPhoneNumber] => 07763059645
)
[1] => SimpleXMLElement Object
(
[RegisteredCharityNumber] => 1188662
[SubsidiaryNumber] => 0
[CharityName] => PALACE THEATRE & OPERA HOUSE TRUST
[MainCharityName] => PALACE THEATRE & OPERA HOUSE TRUST
[RegistrationStatus] => Registered
[PublicEmailAddress] => SheenaWrigley@theambassadors.com
[MainPhoneNumber] => 01612456636
)
我正在尝试通过以下代码打印第一个元素的“慈善机构名称”,但我得到了空值:
print "<br>Result = ". $parser['GetCharitiesByKeywordResponse']['GetCharitiesByKeywordResult']['CharityList']['0']['CharityName'];
需要帮助才能打印出来。
谢谢
【问题讨论】:
想知道为什么有人对我的问题给予负面评价?? 【参考方案1】:它是一个对象,而不是一个数组。
试试这个:
$parser->GetCharitiesByKeywordResponse->GetCharitiesByKeywordResult->CharityList[0]->CharityName;
【讨论】:
以上是关于如何打印 SimpleXMLElement 对象的特定数组元素的主要内容,如果未能解决你的问题,请参考以下文章
多维数组到多维 SimpleXMLElement(XML 对象)[重复]
如何使用 SimpleXmlElement 编写 CDATA?