How To Convert XML To Array with PHP

XML is quite daunting when viewing it for the first time, it’s the heavy tree-like structure can be quite confusing. Sometimes it’s much easier to work with the data from an XML file in the form of an array. How do we convert XML formatted data into an array with PHP? In this article, we will discover how.

First, we need to define some dummy XML data for the purposes of the tutorial.

XML 

The data in the XML is a dummy set of employees across a range of companies. The data shows the first name, last name, email, and company.

<?xml version="1.0" encoding="UTF-8" ?><records>
	<record>
		<first_name>Timon</first_name>
		<last_name>Carson</last_name>
		<email>tempus.non@enimSuspendissealiquet.ca</email>
		<company>Erat Vivamus Inc.</company>
	</record>
	<record>
		<first_name>Boris</first_name>
		<last_name>Alexander</last_name>
		<email>erat@risusNuncac.co.uk</email>
		<company>Nascetur Ridiculus Corp.</company>
	</record>
	<record>
		<first_name>Todd</first_name>
		<last_name>Anthony</last_name>
		<email>pede.Cum@nuncQuisque.net</email>
		<company>Urna Ut Ltd</company>
	</record>
	<record>
		<first_name>Leo</first_name>
		<last_name>Hayes</last_name>
		<email>velit.Pellentesque.ultricies@felispurus.com</email>
		<company>Velit Eget Consulting</company>
	</record>
	<record>
		<first_name>Edan</first_name>
		<last_name>Frederick</last_name>
		<email>pellentesque.Sed@magnaPraesent.net</email>
		<company>Feugiat LLP</company>
	</record>
	<record>
		<first_name>Calvin</first_name>
		<last_name>Joyce</last_name>
		<email>ultrices.posuere.cubilia@pede.com</email>
		<company>Laoreet Lectus Quis Institute</company>
	</record>
	<record>
		<first_name>Germane</first_name>
		<last_name>Gentry</last_name>
		<email>arcu@insodales.net</email>
		<company>Commodo Foundation</company>
	</record>
	<record>
		<first_name>Barclay</first_name>
		<last_name>Buckner</last_name>
		<email>vel@dolorquam.ca</email>
		<company>Quis Massa Consulting</company>
	</record>
	<record>
		<first_name>Glenna</first_name>
		<last_name>Rollins</last_name>
		<email>mi.lacinia.mattis@condimentumDonec.com</email>
		<company>Urna LLP</company>
	</record>
	<record>
		<first_name>Blaze</first_name>
		<last_name>Boyer</last_name>
		<email>eu.ligula@ipsumleo.net</email>
		<company>Quisque Company</company>
	</record>
	<record>
		<first_name>Grady</first_name>
		<last_name>Atkinson</last_name>
		<email>luctus@pedeNunc.net</email>
		<company>Dui Quis Consulting</company>
	</record>
</records>

The most straight forward way to achieve this conversion is with a function named simplexml_load_string. This function interprets XML syntax as a string and converts it to a SimpleXMLElement object type. From the object type, we can use further functions such as encoding and decoding to convert to an array.

Let’s go through these step by step.

Step 1

First, we need to define the XML as a string within the $xml variable. Second, we call to the simplexml_load_string function, passing in the $xml variable.

$xml = '<?xml version="1.0" encoding="UTF-8" ?><records> <record> <first_name>Timon</first_name> <last_name>Carson</last_name> <email>tempus.non@enimSuspendissealiquet.ca</email> <company>Erat Vivamus Inc.</company> </record> <record> <first_name>Boris</first_name> <last_name>Alexander</last_name> <email>erat@risusNuncac.co.uk</email> <company>Nascetur Ridiculus Corp.</company> </record> <record> <first_name>Todd</first_name> <last_name>Anthony</last_name> <email>pede.Cum@nuncQuisque.net</email> <company>Urna Ut Ltd</company> </record> <record> <first_name>Leo</first_name> <last_name>Hayes</last_name> <email>velit.Pellentesque.ultricies@felispurus.com</email> <company>Velit Eget Consulting</company> </record> <record> <first_name>Edan</first_name> <last_name>Frederick</last_name> <email>pellentesque.Sed@magnaPraesent.net</email> <company>Feugiat LLP</company> </record> <record> <first_name>Calvin</first_name> <last_name>Joyce</last_name> <email>ultrices.posuere.cubilia@pede.com</email> <company>Laoreet Lectus Quis Institute</company> </record> <record> <first_name>Germane</first_name> <last_name>Gentry</last_name> <email>arcu@insodales.net</email> <company>Commodo Foundation</company> </record> <record> <first_name>Barclay</first_name> <last_name>Buckner</last_name> <email>vel@dolorquam.ca</email> <company>Quis Massa Consulting</company> </record> <record> <first_name>Glenna</first_name> <last_name>Rollins</last_name> <email>mi.lacinia.mattis@condimentumDonec.com</email> <company>Urna LLP</company> </record> <record> <first_name>Blaze</first_name> <last_name>Boyer</last_name> <email>eu.ligula@ipsumleo.net</email> <company>Quisque Company</company> </record> <record> <first_name>Grady</first_name> <last_name>Atkinson</last_name> <email>luctus@pedeNunc.net</email> <company>Dui Quis Consulting</company> </record> </records>';

$xmlObject = simplexml_load_string($xml);

Let’s see the output of the $xmlObject variable

Output

xml conversion object

Step 2

Now we have the $xmlObject variable, we can use some encoding and decoding to completely convert the XML to array.

$xml = '<?xml version="1.0" encoding="UTF-8" ?><records> <record> <first_name>Timon</first_name> <last_name>Carson</last_name> <email>tempus.non@enimSuspendissealiquet.ca</email> <company>Erat Vivamus Inc.</company> </record> <record> <first_name>Boris</first_name> <last_name>Alexander</last_name> <email>erat@risusNuncac.co.uk</email> <company>Nascetur Ridiculus Corp.</company> </record> <record> <first_name>Todd</first_name> <last_name>Anthony</last_name> <email>pede.Cum@nuncQuisque.net</email> <company>Urna Ut Ltd</company> </record> <record> <first_name>Leo</first_name> <last_name>Hayes</last_name> <email>velit.Pellentesque.ultricies@felispurus.com</email> <company>Velit Eget Consulting</company> </record> <record> <first_name>Edan</first_name> <last_name>Frederick</last_name> <email>pellentesque.Sed@magnaPraesent.net</email> <company>Feugiat LLP</company> </record> <record> <first_name>Calvin</first_name> <last_name>Joyce</last_name> <email>ultrices.posuere.cubilia@pede.com</email> <company>Laoreet Lectus Quis Institute</company> </record> <record> <first_name>Germane</first_name> <last_name>Gentry</last_name> <email>arcu@insodales.net</email> <company>Commodo Foundation</company> </record> <record> <first_name>Barclay</first_name> <last_name>Buckner</last_name> <email>vel@dolorquam.ca</email> <company>Quis Massa Consulting</company> </record> <record> <first_name>Glenna</first_name> <last_name>Rollins</last_name> <email>mi.lacinia.mattis@condimentumDonec.com</email> <company>Urna LLP</company> </record> <record> <first_name>Blaze</first_name> <last_name>Boyer</last_name> <email>eu.ligula@ipsumleo.net</email> <company>Quisque Company</company> </record> <record> <first_name>Grady</first_name> <last_name>Atkinson</last_name> <email>luctus@pedeNunc.net</email> <company>Dui Quis Consulting</company> </record> </records>';
        
$xmlObject = simplexml_load_string($xml);
        
$array = json_decode(json_encode($xmlObject),TRUE); // Convert the object by encoding as json and decoding to associative array.

Now we have a new $array variable, let’s see the contents.

Output

xml to array converted output

 

You can check out this live example in a PHP Fiddle I created for reference.

The SimpleXMLElement class has many more uses in addition to the conversion to array, you can check out the documentation on the function and class used in this tutorial below –

Summary

PHP has a massive arsenal of hidden functionality and in this case, it caters to XML with ease. The classes and functions that are readily available to use are extremely helpful. Make sure you check out the documentation that is listed above if you are frequently working with XML data.

Posted in PHP

Leave a Reply