I'm trying to use unset()
to delete a node in an XML using PHP and can't figure out what is going on here. It doesn't seem to work correctly and I've seen a lot of other questions of similar nature on here but they don't seem to address this issue directly. Here's what my XML looks like:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<user>
<name>Test Name 1</name>
<email>test@test.com</email>
<spouse/>
</user>
<user>
<name>Test Name 2</name>
<email>anotherone@test.com</email>
<spouse>Test Name 3</spouse>
</user>
</data>
My loop that I'm using is like this:
url = 'data/users.xml';
$xml = simplexml_load_file($url);
foreach($xml->user as $theUser){
if($theUser->email[0]=="test@test.com"){
echo "test";
unset($theUser);
}
}
When the e-mail matches "test@test.com" I want to be able to delete that whole user
node. It seems that this should work but I can't figure out why it wouldn't? Any help would be greatly appreciated. Thank you!
SimpleXML
is fine, no need to switch to DOM
, unset()
is working fine, if you do it right:
unset($theUser[0]);
see it working: https://eval.in/228773
However there will be a problem with your foreach()
if you delete a node mid-loop.
I suggest to use xpath()
instead of a loop, IMO elegant and the code is much simpler.
$users = $xml->xpath("/data/user[email='test@test.com']");
will create an array of all <user>
with that email-address.
unset($users[0][0]);
will delete the first user in that array.
foreach ($users as $user) unset($user[0]);
will delete the whole array.
see this in action: https://eval.in/228779