PHP SimpleXML: Remove items with for
php, simplexml, xml
Solution
SimpleXML does not handle node deletion, you need to use DOMNode for this. Happily, when you import your nodes into DOMNode, the instances point to the same tree.
So, you can do that :
<?php
$items = $this->simpleXML->xpath('/rss/channel/item');
foreach ($items as $item) {
$node = dom_import_simplexml($item);
$node->parentNode->removeChild($node);
}
Problem
I just can remove an item from a simpleXML element with: ``` unset($this->simpleXML->channel->item[0]); ``` but I can't with the a for: ``` $items = $this->simpleXML->xpath('/rss/channel/item'); for($i = count($items); $i > $itemsNumber; $i--) { unset($items[$i - 1]); } ``` some items are removed from $items (Netbeans Debug can confirm that) but when I get the path again (/rss/channel/item) nothing was deleted. What's wrong?