Convert XML into Associative Array in PHP

Convert XML into Associative Array in PHP

In this article, we will explain to you how to convert XML into associative array in PHP. sometimes we need to convert XML file into an associative array in PHP. so here we use the file_get_contents() function to read an XML file into PHP. so you can see our following example.

Create XML File

In this step, we will create the XML data file and save the XML file as user.xml. so you can see the following XML code.

Convert XML File Into String

in this step, we convert xml File to string using the file_get_contents() function in php.
Output

Convert XML String to XML Object

Now, we convert xml string to xml object using the simplexml_load_string() function in php.

Output

Convert XML Object to json

we convert xml object to json string using the json_encode() function in php.

Output

Convert json data to array

finally, we will decode JSON data to associative array using json_decode() function.

Example

Output

Read Also
Convert associative arrays into XML in PHP
How to Remove file Extension from Filename in PHP
How to remove duplicate values from an array in PHP

Please follow and like us: