Bazi Bazi - 5 months ago 8
PHP Question

Remove certain elements from XML with PHP

Originally I want to split an XML-File into two files. Therefore I have to remove some elements.

XML looks like this:

<?xml version="1.0"?>
<events>
<event>
<id>1</id>
<typ>new</typ>
</event>
<event>
<id>2</id>
<typ>new</typ>
</event>
<event>
<id>3</id>
<typ>old</typ>
</event>
<event>
<id>4</id>
<typ>new</typ>
</event>
<event>
<id>5</id>
<typ>old</typ>
</event>
<event>
<id>6</id>
<typ>old</typ>
</event>
<event>
<id>7</id>
<typ>old</typ>
</event>
</events>


What I need is:

<?xml version="1.0"?>
<events>
<event>
<id>1</id>
<typ>new</typ>
</event>
<event>
<id>2</id>
<typ>new</typ>
</event>
<event>
<id>4</id>
<typ>new</typ>
</event>
</events>


So I have to remove all events with
typ
=
old
. I've tried it like this:

$events = file_get_contents("events.xml");
$doc = new SimpleXMLElement($events);
foreach($doc->event as $seg)
{
if ($seg->typ == 'new') {
echo "$seg->typ<br />";
unset($doc->event[$seg]);
}
}
echo $doc->asXml();


But only the echo is right, the rest is still the same.
Then I found this function:

function delete_event_typ($typ, $filename = 'events.xml'){
$data = simplexml_load_file($filename);
for($i = 0, $length = count($data->event); $i < $length; $i++){
if($data->event[$i]->typ == $typ){
unset($data->event[$i]);
}
}
file_put_contents($filename, $data->saveXML());
}
//SAMPLE USAGE
delete_event_typ('old');


But it deletes not all entries, it looks like:

<?xml version="1.0"?>
<events>
<event>
<id>1</id>
<typ>new</typ>
</event>
<event>
<id>2</id>
<typ>new</typ>
</event>

<event>
<id>4</id>
<typ>new</typ>
</event>

<event>
<id>6</id>
<typ>old</typ>
</event>

</events>


So what else can I do? Any ideas?

Answer

Make loop in reverse order

for($c = count($doc->event); $c >= 0 ; $c--)
{
  if ($doc->event[$c]->typ == 'old') {
    unset($doc->event[$c]);
  }
}
Comments