saloonphp / xml-wrangler

🌵 XML Wrangler - Easily Read & Write XML in PHP
MIT License
361 stars 14 forks source link

$reader->elements() loosing attributes if node same name #24

Closed SDJeff closed 3 months ago

SDJeff commented 10 months ago
<ORDERPOSITIONS>
    <ORDERPOSITION>
 ...
 <REMARK type="costcenter"/>
 <REMARK type="order"/>
 ...
 </ORDERPOSITION>
</ORDERPOSITIONS>

with $reader->values() the result looks like this

CleanShot 2024-01-04 at 13 00 29

How to keep the attributes as keys?

Sammyjo20 commented 10 months ago

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