note 101014 modified in class.domdocument by danbrown

From: Date: Sat, 20 Nov 2010 15:43:05 +0000
Subject: note 101014 modified in class.domdocument by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174223@lists.php.net to get a copy of this message
A nice and simple node 2 array I wrote, worth a try ;) <?php function getArray($node) { $array = false; if ($node->hasAttributes()) { foreach ($node->attributes as $attr) { $array[$attr->nodeName] = $attr->nodeValue; } } if ($node->hasChildNodes()) { if ($node->childNodes->length == 1) { $array[$node->firstChild->nodeName] = $node->firstChild->nodeValue; } else { foreach ($node->childNodes as $childNode) { if ($childNode->nodeType != XML_TEXT_NODE) { $array[$childNode->nodeName][] = $this->getArray($childNode); } } } } return $array; } ?> --was-- update (old message can be removed?), this one is a few lines smaller :) function getArray($node) { $array = false; if ($node->hasAttributes()) { foreach ($node->attributes as $attr) { $array[$attr->nodeName] = $attr->nodeValue; } } if ($node->hasChildNodes()) { if ($node->childNodes->length == 1) { $array[$node->firstChild->nodeName] = $node->firstChild->nodeValue; } else { foreach ($node->childNodes as $childNode) { if ($childNode->nodeType != XML_TEXT_NODE) { $array[$childNode->nodeName][] = $this->getArray($childNode); } } } } return $array; } http://php.net/manual/en/class.domdocument.php

« previous php.notes (#174223) next »