PHP code example of vaclavvanik / xml-to-array

1. Go to this page and download the library: Download vaclavvanik/xml-to-array library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vaclavvanik / xml-to-array example snippets


use VaclavVanik\XmlToArray\XmlToArray;

$xml = <<<'XML'
<root>
    <good_guy>
        <name>Luke Skywalker</name>
        <weapon>Lightsaber</weapon>
    </good_guy>
    <good_guy>
        <name><![CDATA[<h1>Gandalf</h1>]]></name>
        <weapon>Staff</weapon>
    </good_guy>
    <bad_guy lang="Black Speech">
        <name>Sauron</name>
        <weapon>Evil Eye</weapon>
    </bad_guy>
</root>
XML;

$result = XmlToArray::stringToArray($xml);

[
    'root' => [
        'good_guy' => [
            [
                'name' => 'Luke Skywalker',
                'weapon' => 'Lightsaber',
            ],
            [
                'name' => '<h1>Gandalf</h1>',
                'weapon' => 'Staff',
            ],
        ],
        'bad_guy' => [
            '@attributes' => [
                'lang' => 'Black Speech',
            ],
            'name' => 'Sauron',
            'weapon' => 'Evil Eye',
        ],
    ],
];

use VaclavVanik\XmlToArray\XmlToArray;

$result = XmlToArray::stringToArray('my.xml');

use DOMDocument;
use VaclavVanik\XmlToArray\XmlToArray;

$doc = new DOMDocument();
//$doc->loadXML(...);

$xmlToArray = new XmlToArray($doc);
$result = $xmlToArray->toArray();