PHP code example of vaclavvanik / dom-to-array

1. Go to this page and download the library: Download vaclavvanik/dom-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 / dom-to-array example snippets




declare(strict_types=1);

use DOMDocument;
use VaclavVanik\DomToArray;

$doc = new DOMDocument();
$doc->loadXML('<root/>');

$result = DomToArray::toArray($doc);
// $result = ['root' => ''];

$result = [
    'root' => [
        'name' => 'guy collection',
        'good_guy' => [
            [
                'name' => 'Luke Skywalker',
                'weapon' => 'Lightsaber',
            ],
            [
                'name' => 'Gandalf',
                'weapon' => 'Staff',
            ],
        ],
        'bad_guy' => [
            [
                'name' => 'Saruman',
                'weapon' => 'Staff',
            ],
            [
                'name' => 'Sauron',
                'weapon' => 'Ring',
            ],
        ],
    ],
];

$result = [
    'root' => [
        'single' => '',
        'single@type' => 'any',
        'collection' => [
            ['collection@type' => 'any1'],
            ['collection@type' => 'any2'],
        ],
        'author' => 'Tolkien',
        'author@lang' => 'English',
        'guy' => [
            [
                'name' => 'Sauron',
                'name@weapon' => 'Ring',
                'weapon' => 'Ring',
                'guy@lang' => 'Black Speech',
            ],
                'name' => 'Gandalf',
                'name@weapon' => 'Staff',
                'weapon' => 'Staff',
                'guy@lang' => 'Elvish',
            ],
        ],
        'bad_guy' => [
            [
                'name' => 'Saruman',
                 'name@weapon' => 'Staff',
            ],
            [
                'name' => 'Sauron',
                'name@weapon' => 'Ring',
            ],
        ],
        'bad_guy@lang' => 'Unknown',
    ],
    'root@attr' => 'val',
];

$result = [
    'root' => [
        'good_guy' => [
            'name' => '<h1>Gandalf</h1>',
            'weapon' => 'Staff',
        ],
    ],
];



declare(strict_types=1);

use DOMDocument;
use VaclavVanik\DomToArray;

$domOptions = DomOptions::fromArray([DomOptions::SKIP_ATTRIBUTES => true]);
$result = DomToArray::toArrayWithOptions($doc, $domOptions);

$result = [
    'root' => [
        'single' => '',
        'collection' => ['', ''],
        'author' => 'Tolkien',
        'guy' => [
            [
                'name' => 'Sauron',
                'weapon' => 'Ring',
            ],
            [
                'name' => 'Gandalf',
                'weapon' => 'Staff',
            ],
        ],
        'bad_guy' => [
            'name' => ['Saruman', 'Sauron'],
        ],
    ],
];
xml
<root>
    <name>guy collection</name>
    <good_guy>
        <name>Luke Skywalker</name>
        <weapon>Lightsaber</weapon>
    </good_guy>
    <good_guy>
        <name>Gandalf</name>
        <weapon>Staff</weapon>
    </good_guy>
    <bad_guy>
        <name>Saruman</name>
        <weapon>Staff</weapon>
    </bad_guy>
    <bad_guy>
        <name>Sauron</name>
        <weapon>Ring</weapon>
    </bad_guy>
</root>
xml
<root>
    <good_guy>
        <name><![CDATA[<h1>Gandalf</h1>]]></name>
        <weapon>Staff</weapon>
    </good_guy>
</root>