Sometimes dealing with PHP’s simplexml object can be annoying, and you just wish it was a simple array. There are a ton of functions out there for that, but I’ve found this one works best =)
Source: http://www.php.net/manual/en/ref.simplexml.php
function simplexml2array($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] = $this->simplexml2array($value); } if (isset($a)) $r['@attributes'] = $a; // Attributes return $r; } return (string) $xml; } |
thank you James,
I was looking for somtething like that. It’s perfect
You COPY this code from Daniel FAIVRE 2005, SEE
http://theserverpages.com/php/manual/en/ref.simplexml.php
Your correct, I never claimed i wrote it, but I should have added a link back to the original.
I think you mean “You’re correct”. “Your” means belonging to you as in “Your children”. You’re is short for you are as in “You are correct”.
Now you’re correct!
Thanks man for copying this function.
It works perfectly. You have a great eye for copying things !
LIttle fix:
if (is_object($xml) && get_class($xml) == ‘SimpleXMLElement’) {
Else it could produce warnings…
Awesome! Thanks for the snippet.