Here's a function to convert valid XML to a PHP array:
/**
* Unserializes an XML string, returning a multi-dimensional associative array, optionally runs a callback on
* all non-array data
*
* Notes:
* Root XML tags are stripped
* Due to its recursive nature, unserialize_xml() will also support SimpleXMLElement objects and arrays as input
* Uses simplexml_load_string() for XML parsing, see SimpleXML documentation for more info
*
* @param mixed $input
* @param callback $callback
* @param bool $_recurse used internally, do not pass any value
* @return array|FALSE Returns false on all failure
*/
function xmlToArray( $input, $callback = NULL, $_recurse = FALSE )
{
// Get input, loading an xml string with simplexml if its the top level of recursion
$data = ( ( !$_recurse ) && is_string( $input ) ) ? simplexml_load_string( $input ) : $input;
// Convert SimpleXMLElements to array
if ( $data instanceof SimpleXMLElement ) {
$data = (array) $data;
}
// Recurse into arrays
if ( is_array( $data ) ) foreach ( $data as &$item ) {
$item = xmlToArray( $item, $callback, TRUE );
}
// Run callback and return
return ( !is_array( $data ) && is_callable( $callback ) ) ? call_user_func( $callback, $data ) : $data;
}