simplexml to array
Snippet
Yeah, kitt finished writing this at 10:28 on 19 August 2009
Convert a SimpleXML object into a nested array
/** * @see http://theserverpages.com/php/manual/en/ref.simplexml.php */ function simplexml_to_array($xml) { if (get_class($xml) == 'SimpleXMLElement') { $attributes = $xml->attributes(); foreach($attributes as $k=>$v) { if ($v) $a[$k] = (string) $v; } $x = $xml; $xml = get_object_vars($xml); } if (is_array($xml)) { if (count($xml) == 0) return (string) $x; // for CDATA foreach($xml as $key=>$value) { $r[$key] = simplexml_to_array($value); } if (isset($a)) $r['@'] = $a; // Attributes return $r; } return (string) $xml; }
Add new comment