note 98736 added to book.dom
| From: | Anonymous at osu1 dot php dot net | Date: | Sat, 03 Jul 2010 12:02:38 +0000 |
| Subject: | note 98736 added to book.dom | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170683@lists.php.net to get a copy of this message | ||
Thanks to sweisman at pobox dot com. I modified his function to convert a DOM into an array
structure, which works better for me:
function dom_to_array($root, $full_view=FALSE)
{
$result = array();
// process attributes
if ($root->hasAttributes())
{
$attrs = $root->attributes;
foreach ($attrs as $i => $attr)
{
$result['@attributes'][$attr->name] = $attr->value;
}
}
//process child nodes
$children = $root->childNodes;
for($i = 0; $i < $children->length; $i++)
{
$child = $children->item($i);
if ($child->nodeType == XML_TEXT_NODE)
{
$result['#text'][] = $child->nodeValue;
}
else
{
$result[$child->nodeName][] = dom_to_array($child);
}
}
// direct assignment of text nodes
if (
TRUE
and count($result) == 1
and isset($result['#text'])
and count($result['#text']) == 1
)
{
$result = $result['#text'][0];
}
// reduce array to am more minimalistic structure
if (is_array($result) and !$full_view)
{
foreach ( $result as $key => $value )
{
if ( count($result[$key]) == 1 )
{
$result[$key] = $result[$key][0];
}
}
}
return $result;
}
----
Server IP: 69.147.83.197
Probable Submitter: 92.197.134.14
----
Manual Page -- http://www.php.net/manual/en/book.dom.php
Edit -- https://master.php.net/note/edit/98736
Del: integrated -- https://master.php.net/note/delete/98736/integrated
Del: useless -- https://master.php.net/note/delete/98736/useless
Del: bad code -- https://master.php.net/note/delete/98736/bad+code
Del: spam -- https://master.php.net/note/delete/98736/spam
Del: non-english -- https://master.php.net/note/delete/98736/non-english
Del: in docs -- https://master.php.net/note/delete/98736/in+docs
Del: other reasons-- https://master.php.net/note/delete/98736
Reject -- https://master.php.net/note/reject/98736
Search -- https://master.php.net/manage/user-notes.php