Open In App

Convert multidimensional array to XML file in PHP

Improve
Improve
Like Article
Like
Save
Share
Report

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.




    $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'
            )
        )
    );

    
    

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




    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;
    }

    
    

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




    generateXML($array);

    
    

  • Output:




    <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>

    
    



Last Updated : 12 Jul, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads