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().
functiongenerateXML($data) {    Â   Â$title=$data['company'];   Â$rowCount=count($data['employe']); Â   Â// Create the xml document   Â$xmlDoc=newDOMDocument(); Â   Â$root=$xmlDoc-> appendChild($xmlDoc->                           ÂcreateElement("neveropen"));    Â   Â$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 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:
<neveropen>Â Â Â Â<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></neveropen>
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Here you will find 54216 additional Information on that Topic: geeksforgeeks.org/convert-multidimensional-array-to-xml-file-in-php/ […]
… [Trackback]
[…] Find More on that Topic: geeksforgeeks.org/convert-multidimensional-array-to-xml-file-in-php/ […]
… [Trackback]
[…] Find More Info here on that Topic: geeksforgeeks.org/convert-multidimensional-array-to-xml-file-in-php/ […]