Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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.

TimonCarson[email protected]Erat Vivamus Inc.BorisAlexander[email protected]Nascetur Ridiculus Corp.ToddAnthony[email protected]Urna Ut LtdLeoHayes[email protected]Velit Eget ConsultingEdanFrederick[email protected]Feugiat LLPCalvinJoyce[email protected]Laoreet Lectus Quis InstituteGermaneGentry[email protected]Commodo FoundationBarclayBuckner[email protected]Quis Massa ConsultingGlennaRollins[email protected]Urna LLPBlazeBoyer[email protected]Quisque CompanyGradyAtkinson[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 = 'TimonCarson[email protected]Erat Vivamus Inc.BorisAlexander[email protected]Nascetur Ridiculus Corp.ToddAnthony[email protected]Urna Ut LtdLeoHayes[email protected]Velit Eget ConsultingEdanFrederick[email protected]Feugiat LLPCalvinJoyce[email protected]Laoreet Lectus Quis InstituteGermaneGentry[email protected]Commodo FoundationBarclayBuckner[email protected]Quis Massa ConsultingGlennaRollins[email protected]Urna LLPBlazeBoyer[email protected]Quisque CompanyGradyAtkinson[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 = 'TimonCarson[email protected]Erat Vivamus Inc.BorisAlexander[email protected]Nascetur Ridiculus Corp.ToddAnthony[email protected]Urna Ut LtdLeoHayes[email protected]Velit Eget ConsultingEdanFrederick[email protected]Feugiat LLPCalvinJoyce[email protected]Laoreet Lectus Quis InstituteGermaneGentry[email protected]Commodo FoundationBarclayBuckner[email protected]Quis Massa ConsultingGlennaRollins[email protected]Urna LLPBlazeBoyer[email protected]Quisque CompanyGradyAtkinson[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

Share the post

How To Convert XML To Array with PHP

×

Subscribe to Code Wall - Web Development & Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×