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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version="1.0"?> <user_details> <title>User Details</title> <totalUser>4</totalUser> <user_records> <user> <firstname>john</firstname> <lastname>doe</lastname> <username>john123</username> </user> <user> <firstname>ajay</firstname> <lastname>devgan</lastname> <username>ajdev</username> </user> <user> <firstname>rohit</firstname> <lastname>sharma</lastname> <username>rs123</username> </user> <user> <firstname>kapil</firstname> <lastname>sharma</lastname> <username>ks123</username> </user> </user_records> </user_details> |
Convert XML File Into String
in this step, we convert xml File to string using the file_get_contents() function in php.
Output
1 2 | $xmlFile = "user.xml"; $xmlFileData = file_get_contents($xmlFile); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <user_details> <title>User Details</title> <totalUser>4</totalUser> <user_records> <user> <firstname>john</firstname> <lastname>doe</lastname> <username>john123</username> </user> <user> <firstname>ajay</firstname> <lastname>devgan</lastname> <username>ajdev</username> </user> <user> <firstname>rohit</firstname> <lastname>sharma</lastname> <username>rs123</username> </user> <user> <firstname>kapil</firstname> <lastname>sharma</lastname> <username>ks123</username> </user> </user_records> </user_details> |
Convert XML String to XML Object
Now, we convert xml string to xml object using the simplexml_load_string() function in php.
1 | $xmlDataObject = simplexml_load_string($xmlFileData); |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | SimpleXMLElement Object ( [title] => User Details [totalUser] => 4 [user_records] => SimpleXMLElement Object ( [user] => Array ( [0] => SimpleXMLElement Object ( [firstname] => john [lastname] => doe [username] => john123 ) [1] => SimpleXMLElement Object ( [firstname] => ajay [lastname] => devgan [username] => ajdev ) [2] => SimpleXMLElement Object ( [firstname] => rohit [lastname] => sharma [username] => rs123 ) [3] => SimpleXMLElement Object ( [firstname] => kapil [lastname] => sharma [username] => ks123 ) ) ) ) |
Convert XML Object to json
we convert xml object to json string using the json_encode() function in php.
1 | $jsonData = json_encode($xmlDataObject); |
Output
1 | {"title":"User Details","totalUser":"4","user_records":{"user":[{"firstname":"john","lastname":"doe","username":"john123"},{"firstname":"ajay","lastname":"devgan","username":"ajdev"},{"firstname":"rohit","lastname":"sharma","username":"rs123"},{"firstname":"kapil","lastname":"sharma","username":"ks123"}]}} |
Convert json data to array
finally, we will decode JSON data to associative array using json_decode() function.
1 | $associateArray = json_decode($jsonData, true); |
Example
1 2 3 4 5 6 7 8 9 10 | <?php $xmlFile = "user.xml"; $xmlFileData = file_get_contents($xmlFile); $xmlDataObject = simplexml_load_string($xmlFileData); $jsonData = json_encode($xmlDataObject); $associateArray = json_decode($jsonData, true); echo "<pre>"; print_r($associateArray); echo "</pre>" ?> |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | Array ( [title] => User Details [totalUser] => 4 [user_records] => Array ( [user] => Array ( [0] => Array ( [firstname] => john [lastname] => doe [username] => john123 ) [1] => Array ( [firstname] => ajay [lastname] => devgan [username] => ajdev ) [2] => Array ( [firstname] => rohit [lastname] => sharma [username] => rs123 ) [3] => Array ( [firstname] => kapil [lastname] => sharma [username] => ks123 ) ) ) ) |
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