Re: Re: remove SimpleXML nodes

From: Date: Thu, 31 Aug 2006 23:02:37 +0000
Subject: Re: Re: remove SimpleXML nodes
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-241301@lists.php.net to get a copy of this message
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 (#241301) next »