Introduction to PHP XML into Array
The PHP XML into an array is used to convert an XML document into a PHP array. Sometimes we need to deal with the XML document, as incoming data as an XML document or exporting data as an XML document because the XML language is used to structure the sharing data across the websites or applications. So, the PHP provides XML to array operation to work smoothly with the XML data.
While converting the XML document to an array we need to use some function as mention below –
- file_get_contents() function: This function is uses to read a file as string.
- simplexml_load_string() function: This function is uses to parsing an XML and returnan object.
- json_encode() function: This function is uses to encode a JSON string and return the JSON value.
- json_decode() function: This function is uses to decode a JSON string(PHP variable).
Working of the PHP XML Into Array
The PHP XML into array accepts the XML document and converts it into an array. Suppose we have an XML document as ExXMl.xml, now we want to convert it to an array. So, to convert the XML file into an array we need to follow some steps –
- Import an ExXMl.xml file into PHP with the help of file_get_content() function, so it read the ExXMl.xml file as a string and store it into a variable.
- Next, the read string data is to be converted into an object with the help of the simplexml_load_string() function.
- Next, the converted object is needed to be encoded with the help of json_encode() function, which represents in the json encoded string.
- Finally, use the json_decode() function to decode the json string, which converts the json encoded string into an array.
Examples of PHP XML into Array
Example of PHP XML into an array to load an XML file and convert into a PHP array –
Next, we write the PHP code to understand the PHP XML into array more clearly with the following example, where an XML into an array is used to load XML file which contains student’s information and convert it into a PHP array for farther use, as below –
Example #1
First we create the xml data file by the name “ExXML.xml”, and create the data content as –

4.5 (9,000 ratings)
View Course
Code:
<?xml version="1.0" encoding="utf-8"?>
<Student>
<firstname name = 'John'>
<rollno> 12 </rollno>
<marks> 78 </marks>
<subject> English </subject>
</firstname>
<firstname name='Sam'>
<rollno> 15 </rollno>
<marks> 80 </marks>
<subject> English </subject>
</firstname>
<firstname name='Praveen'>
<rollno> 30 </rollno>
<marks> 50 </marks>
<subject> English </subject>
</firstname>
</Student>
Next,we create the PHP file to read and load the XML file, as below -
<?php
// xml file
$file = "ExXML.xml";
// Read XML file as string
$xml_file = file_get_contents($file);
// xml string is Convert into an object
$obj = simplexml_load_string($xml_file);
// AN object Convert into json string
$json_string = json_encode($obj);
// Convert json string into an array
$array = json_decode($json_string, true);
// print an array
print_r($array);
?>
Output:
As in the above first the XML file is created which contains the student information like name, rollno, marks, and subject. The created XML file is well structured all opening and closing tags are proper. Next the PHP file is created where it read and load the XML file with the help of file_get_contents() andsimplexml_load_string() functions respectively. Next, the loaded object encode and decode as an PHP array with the help ofjson_encode() and json_decode() functions respectively.
Example #2
Example of PHP XML into an array where encounter and parsing errors-
Next, we write the PHP code to understand the PHP XML into an array, where we will see the parsing errors occurred while performing the XML into an array, because the XML tag mismatch or not properly structured, as below –
Code:
<?xml version='1.0'?>
<Student>
<firstname name = 'John'>
<rollno> 12 </rollno>
<marks> 78 </marks>
<!-- missing -->
</firstname>
<firstname name='Sanjay'>
<rollno> 15 </rollno>
<marks> 80 </marks>
<!-- mismatch tag -->
<subject> English <subject>
</firstname>
<firstname name='Parvez'>
<rollno> 30 </rollno>
<marks> 50 </marks>
<subject> English </subject>
</firstname>
</Student>
Next, we create the PHP file to read and load the XML file, as below -
<?php
// xml file
$file = "ExXML.xml";
// Read file as string
$xml_file = file_get_contents($file);
// xml string is Convert into an object
$obj = simplexml_load_string($xml_file);
// check xml load or not
if ($obj == FALSE) {
echo "There were errors to parse the XML file.\n";
exit;
}
// AN object Convert into json string
$json_string = json_encode($obj);
// Convert json string into an array
$array = json_decode($json_string, true);
// print an array
print_r($array);
?>
Output:
As in the above example, the XML file is created, which contains the student information like name, rollno, marks, and subject. The created XML file is not well structured one of the opening and closing tags is not matching and one of the tags is missing(as mention in the XML file with the comment tag). Next the PHP file is created where it read and load the XML file with the help of file_get_contents() andsimplexml_load_string() functions respectively. While parsing and loading the XML file, the warnings are occurred, as we can see in the above output. So, by reading the warnings correct the XML file and load again.
Recommended Articles
This is a guide to PHP XML into Array. Here we also discuss the introduction and working of the php xml into an array along with different examples and its code implementation. You may also have a look at the following articles to learn more –