Getting XML keys and values ​​in PHP

I am working with various XML inputs and outputs and just want to capture the keys and values ​​as you can with an array.

Is there a simple function that can convert the xml to an array or access the keys and values, or am I completely wrong about this?

<?php
$xmlstr = "
<key>
    <parent1>
        <child1>val1</child1>
        <child2>val2</child2>
    </parent1>
    <parent2>val4</parent2>
    <parent3>
        <child1 var="1">
            <gchild>
                <child>value</child>
                <child>value</child>
                <parent>
                    <child>value</child>
                </parent>
            </gchild>
        </child1>
    </parent3>
</key>";

$xml = new SimpleXMLElement($xmlstr);
$xmlarray= convertXMLtoArray($xml);

echo $xmlarray[0]; //outputs: key
echo $xmlarray['key'][1]; //outputs: parent2 or array(child1->val1, child2->...)
echo $xmlarray['key']['parent1']['child1'][0]; //outputs: val1
?>

      

+2


source to share


1 answer


simplexml_load_file



$xml = simplexml_load_file('test.xml');
var_dump($xml);

      

+4


source







All Articles