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
Step 2: Read XML Data
Read the XML file data into string using file_get_contents() function and store into $xmlFileData variable.
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.
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.
Finally, here is the complete code to to read XML file and convert data into associative array in php.
Output 1
Output 2