Convert multidimensional array to XML file in PHP

Given a multi-dimensional array and the task is to convert this array into an XML file. To converting the multi-dimensional array into an xml file, create an XML file and use appendChild() and createElement() function to add array element into XML file.

Example:

  • First, create a PHP Multidimensional Array for converting that array into the XML file format.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    $array = array (
        'company' => 'Gfg',
        'employe' => array (
            '0' => array (
                'name' => 'Jatin Das',
                'age' => '34'
            ),
            '1' => array (
                'name' => 'Mohit Mal',
                'age' => '30'
            ),
            '2' => array (
                'name' => 'Shubham Jha',
                'age' => '24'
            ),
            '3' => array (
                'name' => 'Harsha Bhosle',
                'age' => '29'
            )
        )
    );

    chevron_right

    
    

  • Now, you need to create a user-defined function generatXML().
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    function generateXML($data) {
          
        $title = $data['company'];
        $rowCount = count($data['employe']);
       
        // Create the xml document
        $xmlDoc = new DOMDocument();
       
        $root = $xmlDoc -> appendChild($xmlDoc ->
                                createElement("geeks"));
          
        $root -> appendChild($xmlDoc -> 
                            createElement("title", $title));
        $root -> appendChild($xmlDoc -> 
                      createElement("totalRows", $rowCount));
          
        $tabUsers = $root -> appendChild($xmlDoc ->
                                createElement('rows'));
       
        foreach($data['employe'] as $user) {
            if (!empty($user)) {
                $tabUser = $tabUsers -> appendChild($xmlDoc -> 
                                       createElement('employe'));
                foreach($user as $key => $val) {
                    $tabUser -> appendChild($xmlDoc ->
                                      createElement($key, $val));
                }
            }
        }
       
        header("Content-Type: text/plain");
       
        // Make the output
        $xmlDoc -> formatOutput = true;
       
        // Save xml file
        $file_name = str_replace(' ', '_', $title) . '.xml';
          
        $xmlDoc -> save($file_name);
       
        // Return xml file name
        return $file_name;
    }

    chevron_right

    
    

  • Then use generateXML() function and pass array data in it to convert the array to XML in PHP.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    generateXML($array);

    chevron_right

    
    

  • Output:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <geeks>
        <title>Gfg</title>
        <totalRows>4</totalRows>
        <rows>
            <employe>
                <name>Jatin Das</name>
                <age>34</age>
            </employe>
            <employe>
                <name>Mohit Mal</name>
                <age>30</age>
            </employe>
            <employe>
                <name>Shubham Jha</name>
                <age>24</age>
            </employe>
            <employe>
                <name>Harsha Bhosle</name>
                <age>29</age>
            </employe>
        </rows>
    </geeks>

    chevron_right

    
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.