V
V
Vladislav2020-06-10 18:59:56
PHP
Vladislav, 2020-06-10 18:59:56

How to parse xml in php keeping the order of child elements without concatenating into an array?

I broke my head, I need smart thoughts!

When parsing xml into an array in php, I use the xml->dom->json->array chain. In this case, if the child elements are the same, then they are collected in an array, and if they are different, then in several arrays. The problem is that I need to know exactly the order of the elements in the xml. or save it.

Xml has the following structure:

<m>
    <a>
        <p/>
        <r>7</r>
        <a>
            <d/>
            <r>2</r>
            <r>5</r>
        </a>
    </a>
    <i>p</i>
    <i>p</i>
    <r>3</r>
    <r>3</r>
    <i>p</i>
</m>


I want to get:

[0] => <a>(*?)</a>,
[1] => <i>p<i>,
и т.д.


Or:

<m>
   <a n="0">(*?)</a>
   <i n="1">p</i>
   и т.д.
</m>


The first thing that came to mind was to add an attribute with a number to each child tag. It was not possible to implement, I tried using regular expressions. The second is to parse so that each element is an element in a numbered array, I tried through all the parsing options, it doesn’t work out that way.

I'm new to php, maybe someone has come across this, please tell me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2020-06-12
@SilenceOfWinter

and than simpleXml did not suit? the order does not change, the output is an object that inherits ArrayAccess

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question