php - simpleXML how to access a specific element w

2019-02-19 23:08发布

问题:

<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

回答1:

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



标签: php simplexml