Convert multidimensional array to XML file in PHP

Convert multidimensional array to XML

XML stands for eXtensible Markup Language. In this post you’ll lean about convert multidimensional array to XML. This is a markup language that encodes documents in a machine-readable and human-readable format. XML was designed to store and transport data. XML is often used for distributing data over the Internet. You can use XML many ways in the web application.

If you’re concerned about database size and want to reduce database uses, XML can help you to free the space from the database. Instead of the database you can store the data in the XML file and retrieve data from XML file without connecting to the database. Website sitemap are also generate in XML format.

In this tutorial, we will show you how to convert PHP multidimensional or associative array to the XML file, and the example code shows how to parse the XML file and convert XML data to array in PHP. I have a two-dimensional input array containing the array of key/element pairs.

For better understanding, all the Array to XML conversion code will be grouped together in a PHP function. The generateXML() function converts PHP multidimensional array to XML file format. The data array needs to be passed as a parameter in generateXML() function.

This function creates an XML document using DOMDocument class and inserts the PHP array content in this XML document. In the end, the XML document is saved as an XML file in the specified file location with the given file name.

Muyltidimentnal Array

First of all, Create PHP Multidimensional Array for converting to the XML file format.

Method for generating XML

Now, you need to create a function generatXML()

You only need to use generateXML() function and pass data array in it to convert array to XML in PHP.

The example code will create your Multidimensional Array into the following XML document with your given file name.

Convert XML to PHP Associative Array

For converting XML to PHP Associative Array we will use the few following steps.

  • We will read the XML data from the file and convert the XML to an array using PHP.
  • Read entire file into string using file_get_contents() function in PHP.
  • Convert XML string into an object using simplexml_load_string() function in PHP.
  • Object to JSON convert using json_encode() function.
  • Convert JSON data into associative array using json_decode() function.

For better way, we will create an xml_to_array() function with all these given steps

Simply call this function to convert XML to Multidimensional Array


Output of this function will be like this:

Final code for Convert multidimensional array to XML

Published by Amit Pandey

Professional Android app Developer, Full Stack Web Developer, Blogger, Entrepreneur And Founder of Coding Issue. I am always trying to learn new things or new possible ways to do things.

Did you find this page helpful?


Leave a Reply

Your email address will not be published. Required fields are marked *




One Reply to Post

Neeraj Singh

July 1, 2019

Thanks for posting this article. My problem is solved now.