I’ve been looking for a simple way to convert XML into an array in PHP, like the Perl library XML::Simple does.  The best bet with PHP is SimpleXML, which provides a pretty simple interface for parsing XML in PHP.

Take a simple example XML file:


<list type='randomlist'>
   <item>
       <name>Item Number 1</name>
       <code>ITEM1</code>
   </item>
</list>

To parse this with SimpleXML, do the following:

// If we have the XML in a string we do this:
$xml = new SimpleXMLElement($xmlstr);

// If we have the XML in a file we do this:
$xml = simplexml_load_file($filename);

...

// We can now access the XML values and attributes easily
$name = $xml->list[0]->item->name;
echo $name;   // "Item Number 1"

// Attributes too (these are NOT documented on the PHP site!)
$list_version = $xml->list['type'];
echo $list_verson;  // "random list"

More information is available from the PHP website, and there is a really useful blog here too.