php - simpleXML 如何访问与其他元素同名的特定元素?
<contact:addr>
<contact:street></contact:street>
<contact:street></contact:street>
<contact:street></contact:street>
<contact:city></contact:city>
<contact:pc></contact:pc>
<contact:cc></contact:cc>
</contact:addr>
在上面的例子中,我们可以看到我们的元素街道数量是三倍; 有没有办法通过使用 simpleXML 来正确访问例如第二个街道元素?
提前致谢, MEM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SimpleXML 中的元素引用可以作为数组访问(因为它是迭代器),这意味着
$root->element[1]
将返回根下名为“element”的第二个元素。 (并且[0]
将返回第一个,如 SimpleXML 示例。)您可以使用
foreach($root->element as ..) 迭代所有元素
The element reference in SimpleXML can be accessed as an array (since it is an iterator), meaning that
$root->element[1]
will return the second element with name "element" under the root. (and[0]
will return the first, as shown in the SimpleXML examples in the PHP manual.)You can iterate over all the elements using
foreach($root->element as ..)