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

php – simplexml,返回具有相同标记的多个项目

发布时间:2020-12-13 22:04:14 所属栏目:PHP教程 来源:网络整理
导读:我将以下 XML文件加载到php simplexml中. adfprospectcustomername part="first"Bob/namename part="last"Smith/name/customer/prospect/adf 运用 $customers = new SimpleXMLElement($xmlstring); 这将返回“Bob”,但如何返回姓氏? echo $customers-prospe
我将以下 XML文件加载到php simplexml中.

<adf>
<prospect>
<customer>
<name part="first">Bob</name>
<name part="last">Smith</name>
</customer>
</prospect>
</adf>

运用

$customers = new SimpleXMLElement($xmlstring);

这将返回“Bob”,但如何返回姓氏?

echo $customers->prospect[0]->customer->contact->name;

解决方法

您可以访问不同的< name>数字元素,使用数组样式语法.

$names = $customers->prospect[0]->customer->name;

echo $names[0]; // Bob
echo $names[1]; // Smith

事实上,你已经为< prospect>做了这件事.元件!

另请参见手册中的Basic SimpleXML Usage.

如果要根据某些条件选择元素,则XPath是要使用的工具.

$customer   = $customers->prospect[0]->customer;
$last_names = $customer->xpath('name[@part="last"]'); // always returns an array
echo $last_names[0]; // Smith

(编辑:李大同)

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

    推荐文章
      热点阅读