My xml file looks like this - I want to (simplified for sake of the example ) add 1 to every node value and then check for duplicates. If a duplicate node exists in , I want to remove the parent and then return all with unique as xml.
<?xml version="1.0" encoding="UTF-8"?>
<channel>
<item>
<gid>1240</gid>
</item>
<item>
<gid>1440</gid>
</item>
<item>
<gid>1440</gid>
</item>
<item>
<gid>246</gid>
</item>
So my desired output would be:
<?xml version="1.0" encoding="UTF-8"?>
<channel>
<item>
<gid>1241</gid>
</item>
<item>
<gid>1441</gid>
</item>
<item>
<gid>247</gid>
</item>
My code looks like this - the manipulation part is solved but I cannot figure out how to check if the new is a duplicate and then remove its parent before returning it as XML. I think that storing all values as an array within the loop is correct but after that I get stuck. Thankful for any help.
<?php
$xmllink = 'items.xml';
header("Content-type: text/xml");
$xml=simplexml_load_file($xmllink) or die("Error: Cannot create object");
$xml->channel;
foreach ($xml as $anything) {
// find the <gid>
$newgid = $anything->gid;
// manipulate <gid>
$anything->gid = $newgid +1;
// store all gid + 1 in array
$allgids[] = $newgid;}
echo $xml->asXML();
?>