Xml is the basic element and the child. How to handle the child if they have the same name?

Hello!
The subject in question, but the fact that it is necessary to copy the element and element attributes from one file to another. So element 1 is created, the attributes of schityvat and add, 2 the element is created, attributes are read, but since such elements 2 is copied from the first. Work parts with identical names and attributes in the output.
How to iterate over this assots. array if the keys are the same and apply it to what you need? To 3, 4, 5...?
June 8th 19 at 16:44
1 answer
June 8th 19 at 16:46
$catalog = new SimpleXMLElement(file_get_contents($file));
 foreach($catalog->stores->store as $arStore){
 $name = (string)$arStore['name'];
 }


the structure of xml is such
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<stores>
 <store uid="111" name="444">
 <store uid="222" name="555">
 <store uid="333" name="666">
 </store></store></store></stores></catalog>
What is in $catalog->stores->store? If always taken 1st element, so not correctly parse the xml - jett_Hilpert commented on June 8th 19 at 16:49
here read store - created in the resulting xml is similar to
read its attributes (and how to read the attributes of the first store? I loop through the elements and I have read both (and in this case, 3 elements, attributes)) - added to store in the output file
read 2 store - set up in RF.
read the attributes added to store 2-mu in the R. f.
It's logic, but in practice the first element of the array with the key store and its attributes are transferred to all store elements of the result file - nikita.Stracke commented on June 8th 19 at 16:52
added an answer using SimpleXMLElement
in $catalog->stores->store stores the current item, when working in a loop the current element is updated to the next
and in the cycle of moving things in the right order

not quite understand what you need, but before the foreach loop, declare the counter which record the current iteration and on this basis to do the necessary manipulations with the data.
from the first store, you can read the attributes
so
echo (string)$catalog->stores->store['name']
or so
echo (string)$catalog->stores->store[0]['name'] - Rose_Rodrigu commented on June 8th 19 at 16:55
Does not work.
Could You throw a working code (example)
Here's the file file1.xml copy the bitmap to a file file2.xml with all the attributes.
Then I could broadcast on your work your decision.
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<stores>
 <store uid="111" name="444">
 <store uid="222" name="555">
 <store uid="333" name="666">
</store></store></store></stores>
</catalog>
- nikita.Stracke commented on June 8th 19 at 16:58

Find more questions by tags XMLPHP