Re: Re: remove SimpleXML nodes

From: Date: Fri, 01 Sep 2006 08:03:37 +0000
Subject: Re: Re: remove SimpleXML nodes
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-241313@lists.php.net to get a copy of this message
Thanks a lot to all :) Using DOM I can do what I need. Anyway I was trying the proposed solutions with SimpleXML but none of them worked for me :( I tried splitting $key and $value in the foreach command, but when I dump the xml (with the asXML function) I still have the nodes that I supposed to delete... And the same using references, cause I think using unset with referenced variable names doesn't delete the referenced var, it just deletes the link to the referenced var... Concretely I tried: TEST ONE: foreach ($xmlDatabase as $key => $oneTable) if ($oneTable['name'] == 'one') unset($xmlDatabase[$key]); $sourceXML = $xmlDatabase->asXML(); // <----- node <table name='one'> is still there TEST TWO foreach ($xmlDatabase as $key => &$oneTable) if ($oneTable['name'] == 'one') unset($oneTable); $sourceXML = $xmlDatabase->asXML(); // <----- node <table name='one'> is still there If there's no luck with xml I will use of course DOM for this, but I'm really curious now :P Again, thanks a lot for the help. Javi Ruiz. On 9/1/06, Curt Zirzow <czirzow@gmail.com> wrote: On 8/31/06, Adam Zey <azey@nit.ca> wrote:
Javier Ruiz wrote: Hi all, So I want to do... $xmlDatabase = new SimpleXMLElement($myXML); foreach ($xmlDatabase as $oneTable) { if ($oneTable['name'] == 'two') {
     ///  ---- HERE I WANT TO DELETE THE $oneTable NODE
     unset($oneTable);   // <-- and this doesn't work...
} } any ideas? The answer is simple, everybody else seems to have missed it. foreach() makes a copy of the array before working on it. Changes made to $oneTable would obviously never affect the original. You're just unsetting a temporary variable that's going to be overwritten next time through the loop anyhow. You should have better luck with this code: $xmlDatabase = new SimpleXMLElement($myXML); foreach ($xmlDatabase as $key => $oneTable)
fwiw, In php5 you can do something like: foreach ($xmlDatabase as $key => &$oneTable)
                //note the & ------------------^^^^
unset($oneTable);

« previous php.general (#241313) next »