php - simpleXML 如何访问与其他元素同名的特定元素?

发布于 2024-09-18 02:00:44 字数 425 浏览 2 评论 0 原文

<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

<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>

On the example above we can see that we do have three times the element street;
Is there a way, by using simpleXML, to properly access, for example, the second street element?

Thanks in advance,
MEM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-09-25 02:00:44

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 ..)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文