Exmaple – Convert array to XML in PHP

107

Many times we need store the data as a XML into the database or into the file for later use. For fulfil this need, we will need to convert the data to XML and save the XML file. In this tutorial we will discuss, how to create XML from array in PHP. We have created a simple script for convert PHP array to XML. You can easily generate XML file from PHP array and save the XML file. You can convert all types of array like Associative array or Multidimensional arrays.

PHP Array:

At first we will store the users data into a variable ($users_array).

Array to XML:

Now we will convert the users array to XML using PHP SimpleXML. Please follow the comment tags for better understand.

XML File:

The users.xml file contains the following xml.

Insert XML Into Databse

If you want to save the XML into the database, then replace the $xml_file variable line with the following code line. Now you can insert $xml_file variable into the database.

 

Leave A Reply

Your email address will not be published.