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.
Timon Carson [email protected] Erat Vivamus Inc. Boris Alexander [email protected] Nascetur Ridiculus Corp. Todd Anthony [email protected] Urna Ut Ltd Leo Hayes [email protected] Velit Eget Consulting Edan Frederick [email protected] Feugiat LLP Calvin Joyce [email protected] Laoreet Lectus Quis Institute Germane Gentry [email protected] Commodo Foundation Barclay Buckner [email protected] Quis Massa Consulting Glenna Rollins [email protected] Urna LLP Blaze Boyer [email protected] Quisque Company Grady Atkinson [email protected] Dui Quis Consulting
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 = 'Timon Carson [email protected] Erat Vivamus Inc. Boris Alexander [email protected] Nascetur Ridiculus Corp. Todd Anthony [email protected] Urna Ut Ltd Leo Hayes [email protected] Velit Eget Consulting Edan Frederick [email protected] Feugiat LLP Calvin Joyce [email protected] Laoreet Lectus Quis Institute Germane Gentry [email protected] Commodo Foundation Barclay Buckner [email protected] Quis Massa Consulting Glenna Rollins [email protected] Urna LLP Blaze Boyer [email protected] Quisque Company Grady Atkinson [email protected] Dui Quis Consulting ';
$xmlObject = simplexml_load_string($xml);
Let’s see the output of the $xmlObject
variable
Output
Step 2
Now we have the $xmlObject
variable, we can use some encoding and decoding to completely convert the XML to array
.
$xml = 'Timon Carson [email protected] Erat Vivamus Inc. Boris Alexander [email protected] Nascetur Ridiculus Corp. Todd Anthony [email protected] Urna Ut Ltd Leo Hayes [email protected] Velit Eget Consulting Edan Frederick [email protected] Feugiat LLP Calvin Joyce [email protected] Laoreet Lectus Quis Institute Germane Gentry [email protected] Commodo Foundation Barclay Buckner [email protected] Quis Massa Consulting Glenna Rollins [email protected] Urna LLP Blaze Boyer [email protected] Quisque Company Grady Atkinson [email protected] Dui Quis Consulting ';
$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
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 –
- simplexml_load_string
- SimpleXMLElement
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.
The post How To Convert XML To Array with PHP appeared first on Code Wall.
This post first appeared on Code Wall - Web Development & Programming, please read the originial post: here