Iterate over an object and convert to an array type

I have an XML class that reads an XML file and allows it to access, like this

<config>
    <directory>
        <mvc>
             <model>model</model>
        </mvc>
    </directory>
</config>

echo $xml->config->directory->mvc->model

(if the object is passed to a variable $xml)

exits

model

I want to turn each node parent / child / value into an array. So

echo $xmlArr['config']['directory']['mvc']['model']

echo model

I was in the process of using a loop foreach, but then I realized that if the XML file has a deep level of nested nodes, then my foreach statements will cover only as many levels. XML files are a dynamic variable for which I can’t do the accounting, and I need a dynamic method that allows me to recursively pass through nodes and add them as an array index.

+3
source share

All Articles