xml-wrangler
xml-wrangler copied to clipboard
$reader->elements() loosing attributes if node same name
<ORDERPOSITIONS>
<ORDERPOSITION>
...
<REMARK type="costcenter"/>
<REMARK type="order"/>
...
</ORDERPOSITION>
</ORDERPOSITIONS>
with $reader->values()
the result looks like this
How to keep the attributes as keys?
Hey @SDJeff!
This is the expected behaviour - when there are multiple child elements with the same name, the library will decode it into an array of values. If you are looking to have the attributes on the element, you can try using $reader->elements() instead which is slightly harder to traverse, but gives you an array of Element classes which contains all of the information.
You could also read the REMARK property separately with the $reader->element() method. You can use dot-notation to find the node:
$elements = $reader->element('orderpositions.remark')->get(); // [Element::class, Element::class]
foreach($elements as $element) {
$attributes = $element->getAttributes(); // e.g 'type' => 'order'
}