note 103306 added to book.simplexml
| From: | marcus dot garcia at uol dot com dot br | Date: | Wed, 06 Apr 2011 14:14:44 +0000 |
| Subject: | note 103306 added to book.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178561@lists.php.net to get a copy of this message | ||
Revised version, as i've got a comment pointing out a bug (in fact even in my post the bug was
there and i didn't see, the very first attribute key was being discarded):
function xml2array($xml) {
if(get_class($xml) != 'SimpleXMLElement') {
$xml = simplexml_load_string($xml);
}
if(!$xml) {
return false;
}
$main = $xml->getName();
$arr = array();
$nodes = $xml->children();
foreach($nodes as $node) {
$nodeName = $node->getName();
$nodeAttributes = $node->attributes();
$attributesArray = array();
foreach($nodeAttributes as $attributeName => $attributeValue) {
$attributesArray[$attributeName] = (string) $attributeValue;
}
$nodeValue = sizeOf($node->children()) == 0 ? trim($node) : xml2array($node);
if(!isSet($arr[$nodeName]['value'])) {
$arr[$nodeName]['value'] = $nodeValue;
} else {
if(!is_array($arr[$nodeName]['value'])) {
$arr[$nodeName]['value'][] = array_shift($arr[$nodeName]);
}
$arr[$nodeName]['value'][] = $nodeValue;
}
$arr[$nodeName]['attributes'][] = $attributesArray;
}
return($arr);
}
----
Server IP: 69.147.83.197
Probable Submitter: 201.83.61.187
----
Manual Page -- http://www.php.net/manual/en/book.simplexml.php
Edit -- https://master.php.net/note/edit/103306
Del: integrated -- https://master.php.net/note/delete/103306/integrated
Del: useless -- https://master.php.net/note/delete/103306/useless
Del: bad code -- https://master.php.net/note/delete/103306/bad+code
Del: spam -- https://master.php.net/note/delete/103306/spam
Del: non-english -- https://master.php.net/note/delete/103306/non-english
Del: in docs -- https://master.php.net/note/delete/103306/in+docs
Del: other reasons-- https://master.php.net/note/delete/103306
Reject -- https://master.php.net/note/reject/103306
Search -- https://master.php.net/manage/user-notes.php