Convert XML into Associative Array in PHP

XML (Extensible Markup Language) is a markup language that is widely used to store data in a machine and human readable format. We can easily read and write data from XML file to use in our web applications.

If you’re developing a web application and wants to store data to a file instead of database, then XML is the best solution. You can write the data to the XML file and then retrieve the data from XML file to use in your web application.

So if you’re looking for solution to store data into XML file and wants retrieve data from XML file then you’re here at right place. In our previous tutorial you have learned about Control Statements in PHP. In this tutorial we will show you how to parse the XML file and convert XML file data to PHP associative or multidimensional array to use into web applications.

Also, read:

Convert XML to PHP Associative Array

Here we will read the XML data file and convert XML data to associative arry.


Step1: Create XML Data File
First we will create XML data file using following XML. We will save XML file as employee.xml

<?xml version="1.0"?>
<employee_details>
  <title>Employee Details</title>
  <totalEmployee>3</totalEmployee>
  <emp_records>
    <employee>
      <name>William Smith</name>
      <email>william@phpzag.com</email>
    </employee>
    <employee>
      <name>Andy Flower</name>
      <email>andy@phpzag.com</email>
    </employee>
    <employee>
      <name>Cris Jhon</name>
      <email>jhon@phpzag.com</email>
    </employee>
  </emp_records>
</employee_details>

Step2: Read XML Data File
We will read the XML file data into string using file_get_contents() function and store into $xmlFileData variable.

$xmlFile = "employee.xml";
$xmlFileData = file_get_contents($xmlFile);

Step3: Convert XML string Data into XML Object
We will convert XML string data to XML Data object using simplexml_load_string() PHP function and store into $xmlDataObject variable.

$xmlDataObject = simplexml_load_string($xmlFileData);

Step4: Convert XML Data object to Associative Array
Now we will convert xml data object to associative array. So first we will encode XML data object to JSON using json_encode() PHP function and store into $jsonData variable. Then finally we will decode JSON data using json_decode() PHP function to convert to associative array.

$jsonData  = json_encode($xmlDataObject);
$associateArray = json_decode($jsonData, true);	

Here is the complete code to to read XML file and convert data into associative array.


<?php
$xmlFile = "employee.xml";
$xmlFileData = file_get_contents($xmlFile);
$xmlDataObject = simplexml_load_string($xmlFileData);
$jsonData  = json_encode($xmlDataObject);
$associateArray = json_decode($jsonData, true);	
print_r($associateArray);
?>

Here is the associative array by reading XML file after running above:

Array
(
    [title] => Employee Details
    [totalEmployee] => 3
    [emp_records] => Array
        (
            [employee] => Array
                (
                    [0] => Array
                        (
                            [name] => William Smith
                            [email] => william@phpzag.com
                        )

                    [1] => Array
                        (
                            [name] => Andy Flower
                            [email] => andy@phpzag.com
                        )

                    [2] => Array
                        (
                            [name] => Cris Jhon
                            [email] => jhon@phpzag.com
                        )

                )

        )

)

You may also like: