note 84257 added to domnode.removechild
| From: | (DigitalGemstones.com) | Date: | Sat, 05 Jul 2008 23:15:14 +0000 |
| Subject: | note 84257 added to domnode.removechild | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141209@lists.php.net to get a copy of this message | ||
Note that iterating through the childNodes array and removing those children will stop the
iteration.
For example:
foreach($node->childNodes as $child)
{
if(iWantToDeleteThisNode($child))
$child->parentNode->removeChild($child);
}
Will not work. Note that in removing the node, the childNodes array gets rebuilt (it seems) and so
only the first item will be removed. In order to properly remove all the children you want to
remove, you will need to do something like the following:
$nodesToDelete = array();
foreach($node->childNodes as $child)
{
if(iWantToDeleteThisNode($child))
$nodesToDelete[] = $child;
}
foreach($nodesToDelete as $node)
{
$node->parentNode->removeChild($node);
}
I believe this is actually more efficient than the first snippet would be (if it worked) but
obviously I cannot benchmark it.
----
Server IP: 64.71.164.2
Probable Submitter: 76.126.98.216
----
Manual Page -- http://www.php.net/manual/en/domnode.removechild.php
Edit -- https://master.php.net/note/edit/84257
Del: integrated -- https://master.php.net/note/delete/84257/integrated
Del: useless -- https://master.php.net/note/delete/84257/useless
Del: bad code -- https://master.php.net/note/delete/84257/bad+code
Del: spam -- https://master.php.net/note/delete/84257/spam
Del: non-english -- https://master.php.net/note/delete/84257/non-english
Del: in docs -- https://master.php.net/note/delete/84257/in+docs
Del: other reasons-- https://master.php.net/note/delete/84257
Reject -- https://master.php.net/note/reject/84257
Search -- https://master.php.net/manage/user-notes.php