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. so we will give you a simple example of an XML to 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

Please follow and like us: