开发者

php - simpleXML how to access a specific element with the same name as others?

开发者 https://www.devze.com 2023-01-14 20:15 出处:网络
<contact:addr> <contact:street></contact:street> <contact:street></contact:street>
<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 simpleX开发者_如何学GoML, to properly access, for example, the second street element?

Thanks in advance, MEM


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

0

精彩评论

暂无评论...
验证码 换一张
取 消