N-Bomb(Nerd) Posted December 17, 2009 Share Posted December 17, 2009 Hi, I'm new to SimpleXML and I'm trying to learn. I'm trying to figure out how to delete a child ( and all sub-children ), however I'm not doing very good. Was hoping someone could explain to me what I'm doing wrong.. test.xml: <?xml version="1.0" encoding="ISO-8859-1"?> <man> <phone company="verizon"> <r>empty</r> </phone> <phone company="att"> <r>empty</r> </phone> <phone> <r>empty</r> </phone> </man> test.php $phones = array("verizon"); $xml = simplexml_load_file('test.xml'); foreach ($xml as $phone) { if (in_array((string) $phone['company'], $phones)) { unset($xml->$phone); } } print_r($xml); As you can see the phone with the company = "verizon" is still in the xml.. how do I get it to remove? Thanks Link to comment https://forums.phpfreaks.com/topic/185511-simplexml-question/ Share on other sites More sharing options...
rajivgonsalves Posted December 17, 2009 Share Posted December 17, 2009 your loop should be for ($i=0;$i<count($xml->phone);$i++) { if (in_array((string) $xml->phone[$i]['company'], $phones)) { unset($xml->phone[$i]); } } Link to comment https://forums.phpfreaks.com/topic/185511-simplexml-question/#findComment-979439 Share on other sites More sharing options...
JAY6390 Posted December 17, 2009 Share Posted December 17, 2009 http://php.net/manual/pt_BR/domnode.removechild.php http://www.w3schools.com/Dom/dom_nodes_remove.asp Link to comment https://forums.phpfreaks.com/topic/185511-simplexml-question/#findComment-979443 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.