note 93717 added to book.dom
| From: | sweisman at pobox dot com | Date: | Thu, 24 Sep 2009 08:19:25 +0000 |
| Subject: | note 93717 added to book.dom | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161008@lists.php.net to get a copy of this message | ||
I had problems with the dom2array_full function by "nospam at ya dot ru". Here's my
function, which works correctly for my project, and might work for yours:
function dom_to_array($root)
{
$result = array();
if ($root->hasAttributes())
{
$attrs = $root->attributes;
foreach ($attrs as $i => $attr)
$result[$attr->name] = $attr->value;
}
$children = $root->childNodes;
if ($children->length == 1)
{
$child = $children->item(0);
if ($child->nodeType == XML_TEXT_NODE)
{
$result['_value'] = $child->nodeValue;
if (count($result) == 1)
return $result['_value'];
else
return $result;
}
}
$group = array();
for($i = 0; $i < $children->length; $i++)
{
$child = $children->item($i);
if (!isset($result[$child->nodeName]))
$result[$child->nodeName] = dom_to_array($child);
else
{
if (!isset($group[$child->nodeName]))
{
$tmp = $result[$child->nodeName];
$result[$child->nodeName] = array($tmp);
$group[$child->nodeName] = 1;
}
$result[$child->nodeName][] = dom_to_array($child);
}
}
return $result;
}
----
Server IP: 82.80.248.162
Probable Submitter: 192.115.132.230
----
Manual Page -- http://www.php.net/manual/en/book.dom.php
Edit -- https://master.php.net/note/edit/93717
Del: integrated -- https://master.php.net/note/delete/93717/integrated
Del: useless -- https://master.php.net/note/delete/93717/useless
Del: bad code -- https://master.php.net/note/delete/93717/bad+code
Del: spam -- https://master.php.net/note/delete/93717/spam
Del: non-english -- https://master.php.net/note/delete/93717/non-english
Del: in docs -- https://master.php.net/note/delete/93717/in+docs
Del: other reasons-- https://master.php.net/note/delete/93717
Reject -- https://master.php.net/note/reject/93717
Search -- https://master.php.net/manage/user-notes.php