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')));chevron_rightfilter_none - Now, you need to create a user-defined function generatXML().
functiongenerateXML($data) {$title=$data['company'];$rowCount=count($data['employe']);// Create the xml document$xmlDoc=newDOMDocument();$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($useras$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 namereturn$file_name;}chevron_rightfilter_none - Then use generateXML() function and pass array data in it to convert the array to XML in PHP.
generateXML($array);chevron_rightfilter_none - 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>chevron_rightfilter_none
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to convert XML file into array in PHP?
- How to generate an XML file dynamically using PHP?
- PHP multidimensional array search by value
- How to search by key=>value in a multidimensional array in PHP ?
- Sort a multidimensional array by date element in PHP
- How to check an array is multidimensional or not in PHP ?
- How to merge the duplicate value in multidimensional array in PHP ?
- Multidimensional Associative Array in PHP
- Multidimensional arrays in PHP
- PHP | XMLReader XML() Function
- How to parse and process HTML/XML using PHP ?
- Multidimensional Arrays in Java
- How to convert an array to CSV file in PHP ?
- Web technologies | HTML and XML | Question 1
- Displaying XML Using CSS
- Java Architecture for XML Binding ( JAXB ) | Set-1
- Displaying XML Using XSLT
- Difference between JSON and XML
- XML | Syntax
- XML | Tags
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.

