Convert XML into Associative Array in PHP
XML (Extensible Markup Language) is a markup language that encodes documents in a machine-readable and human-readable format.We can easily read and write data from XML file to use in our web applications. The XML file will import into PHP using file_get_contents() function which read the entire file and store into an array. In this tutorial, we have shared how to parse the XML file and convert XML file data to PHP associative or multidimensional array to use in web applications.
Step 1: XML data file and convert XML data to associative array.
Create XML data file using following XML. We will save XML file as student.xml
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 |
<?xml version="1.0"?> <student_details> <title>Student Details</title> <totalStudent>5</totalStudent> <student_records> <student> <name>Tom</name> <email>tom@techarise.com</email> <age>15</age> </student> <student> <name>Jerry</name> <email>jerry@techarise.com</email> <age>17</age> </student> <student> <name>James</name> <email>james@techarise.com</email> <age>14</age> </student> <student> <name>Wood</name> <email>wood@techarise.com</email> <age>16</age> </student> <student> <name>Adam</name> <email>adam@techarise.com</email> <age>18</age> </student> </student_records> </student_details> |
Step 2: Read XML Data
Read the XML file data into string using file_get_contents() function and store into $xmlFileData variable.
1 2 |
$xmlFile = "student.xml"; $xmlFileData = file_get_contents($xmlFile); |
Step 3: Convert XML string Data into XML Object
Convert XML string data to XML Data object using simplexml_load_string() PHP function and store into $xmlDataObject variable.
1 |
$xmlDataObject = simplexml_load_string($xmlFileData); |
Step 4: Convert XML Data object to Associative Array
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.
1 2 |
$jsonData = json_encode($xmlDataObject); $associateArray = json_decode($jsonData, true); |
Finally, here is the complete code to to read XML file and convert data into associative array in php.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $xmlFile = "xml_file/student.xml"; $xmlFileData = file_get_contents($xmlFile); $xmlDataObject = simplexml_load_string($xmlFileData); $jsonData = json_encode($xmlDataObject); // output 1 print_r($jsonData); $associateArray = json_decode($jsonData, true); // output 2 print_r($associateArray); ?> |
Output 1
1 |
{"title":"Student Details","totalStudent":"5","student_records":{"student":[{"name":"Tom","email":"topm@techarise.com","age":"15"},{"name":"Jerry","email":"jerry@techarise.com","age":"17"},{"name":"James","email":"james@techarise.com","age":"14"},{"name":"Wood","email":"wood@techarise.com","age":"16"},{"name":"Adam","email":"adam@techarise.com","age":"18"}]}} |
Output 2
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 42 43 44 45 46 47 48 |
Array ( [title] => Student Details [totalStudent] => 5 [student_records] => Array ( [student] => Array ( [0] => Array ( [name] => Tom [email] => topm@techarise.com [age] => 15 ) [1] => Array ( [name] => Jerry [email] => jerry@techarise.com [age] => 17 ) [2] => Array ( [name] => James [email] => james@techarise.com [age] => 14 ) [3] => Array ( [name] => Wood [email] => wood@techarise.com [age] => 16 ) [4] => Array ( [name] => Adam [email] => adam@techarise.com [age] => 18 ) ) ) ) |