PHP code example of openprunus / php-xml-array

1. Go to this page and download the library: Download openprunus/php-xml-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/ */

    

openprunus / php-xml-array example snippets



use \PhpXmlArray\XML;

$array = [
    "Root" => [
        [
            "Level1" => "value"
        ],
        [
            "Level1" => [
                [
                    "Level2" => "Level 2 value"
                ],
                [
                    "Level2" => [
                        "_attributes" => [
                            "attrbidule" => "toto"
                        ],
                        "_value" => "value de Level 2"
                    ]
                ],
                "AutreBalise" => "valeur"
            ]
        ],
        [
            "Level1" => [
                "_attributes" => [
                    "attrLevel1" => "titi"
                ],
                "_value" => "value"
            ]
        ]
    ]
];

$xmlInstance = new Xml();

// For unformatted XML
$unformattedConvertedXmlToArray = $xmlInstance->xmlFromArray($array);

// For formatted XML
$formattedConvertedXmlToArray = $xmlInstance->xmlFromArray($array, true);



$array1 = [
    "Name" => "Doe",
    "FirstName" => "John",
    "Age" => 30
];

/*
<Name>Doe</Name>
<FirstName>John</FirstName>
<Age>30</Age>
*/

$array2 = [
    "Root" => [
        "Name" => "Doe",
        "FirstNames" => [
            "_attributes" => [
                    "foo" => "bar"
            ],
            "_value" => [
                ["FirstName" => "Toto"],
                ["FirstName" => "Titi"],
                ["FirstName" => "Tata"]
            ]
        ],
        "Ages" => [
            "Age1" => 30,
            "Age2" => 31,
            "Age3" => 32
        ]
    ]
];
/*
<Root>
    <Name>Doe</Name>
    <FisrtNames foo="bar">
        <FirstName>Toto</FirstName>
        <FirstName>Titi</FirstName>
        <FirstName>Tata</FirstName>
    </FirstNames
    <Ages>
        <Age1>30</Age1>
        <Age2>31</Age2>
        <Age3>32</Age3>
    </Ages>
</Root>
*/

$array3 = [
    "Root" => [
        "_attributes" => [
            "test" => "value"
        ],
        "_value" => [
            "Level1" => [
                "Level2" => [
                    "Level3" => "test"
                ]
            ]
        ]
    ]
];

/*
<Root test="value">
    <Level1>
        <Level2>
            <Level3>test</Level3>
        </Level2>
    </Level1>
</Root>
*/

$array4 = [
    "Root" => [
        "Alone"
    ]
];

/*
<Root>
    <Alone />
</Root>
*/



composer