Skip to content

Instantly share code, notes, and snippets.

@andizzle
Created August 16, 2012 02:40
Show Gist options
  • Save andizzle/3365885 to your computer and use it in GitHub Desktop.
Save andizzle/3365885 to your computer and use it in GitHub Desktop.
SOAP XML parser
<?php
class SOAP_parser {
/**
* Convert an XML (SOAP) document to a multi dimensional array
* Pass in an XML document (or SimpleXMLElement object) and this recrusively loops through and builds a representative array
*
* @param string $xml - XML document - can optionally be a SimpleXMLElement object
* @return array ARRAY
*/
function toArray($xml) {
if (is_string($xml)){
$xml = str_replace('soap:', "soap-", $xml);
$xml = new SimpleXMLElement($xml);
}
$children = $xml->children();
if (!$children)
return (string) $xml;
$arr = array();
foreach ($children as $key => $node) {
$node = SOAP_parser::toArray($node);
// support for 'anon' non-associative arrays
if ($key == 'anon')
$key = count($arr);
// if the node is already set, put it into an array
if (array_key_exists($key, $arr) && isset($arr[$key])) {
if (!is_array($arr[$key]) || !array_key_exists(0, $arr[$key]) || ( array_key_exists(0, $arr[$key]) && ($arr[$key][0] == null)))
$arr[$key] = array($arr[$key]);
$arr[$key][] = $node;
} else {
$arr[$key] = $node;
}
}
return $arr;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment