title: How to merge multiple XML files in PHP
body: <p>I am new to PHP programming and would appreciate whatever help i can get. I have an Xml file that looks like below..</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
        &lt;results&gt;
          &lt;test external_id="123"&gt;
             &lt;result&gt;p&lt;/result&gt;
          &lt;/test&gt;
          &lt;test external_id="1237"&gt;
             &lt;result&gt;p&lt;/result&gt;
          &lt;/test&gt;
          &lt;test external_id="234"&gt;
             &lt;result&gt;p&lt;/result&gt;
          &lt;/test&gt;
          &lt;test external_id="678"&gt;
              &lt;result&gt;p&lt;/result&gt;
          &lt;/test&gt;
       &lt;/results&gt;
  </code></pre>
  
  <p>The XML file is generated from a testrun. Now sometimes the test could break and would restart generating another xml file which might contain similar values as generated in the first xml file. For example, Xml file 2 might contain the following values  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;results&gt;
       &lt;test external_id="412"&gt;
          &lt;result&gt;p&lt;/result&gt;
       &lt;/test&gt;
       &lt;test external_id="234"&gt;
          &lt;result&gt;p&lt;/result&gt;
       &lt;/test&gt;
       &lt;test external_id="123"&gt;
          &lt;result&gt;p&lt;/result&gt;
       &lt;/test&gt;
       &lt;test external_id="745"&gt;
          &lt;result&gt;p&lt;/result&gt;
       &lt;/test&gt;
      &lt;/results&gt;
  </code></pre>
  
  <p>I need to write a PHP script to merge the different XML files generated into a single XML file without duplicates. Only one occurrence of (test external_id = "XXX")should be in the final xml file. There could be more than 2 XML files generated during the test run. I have searched through the various solutions given on stackoverflow but can't get it. I have been able to merge the files but the duplicates are still there.What I have written so far. Thanks for your anticipated help. </p>
  
  <pre><code>&lt;?php
  
  $doc1 = new DOMDocument();
  $doc1-&gt;load('11.xml');
  
  $doc2 = new DOMDocument();
  $doc2-&gt;load('12.xml');
  
  // get 'results' element of document 1
  $res1 = $doc1-&gt;getElementsByTagName('results')-&gt;item(0);
  
  // iterate over 'testcase' elements of document 2
  $items2 = $doc2-&gt;getElementsByTagName('test');
  for ($i = 0; $i &lt; $items2-&gt;length; $i ++) {
          $item2 = $items2-&gt;item($i);
  
  // import/copy item from document 2 to document 1
   $item1 = $doc1-&gt;importNode($item2, true);
  
  // append imported item to document 1 'res' element
  $res1-&gt;appendChild($item1);
  
      }
  
  $doc1-&gt;save('merged.xml'); //saving into xml file
  
  
  $seen=array();
  
  $doc3 = new DOMDocument();
  $doc3-&gt;load('merged.xml');
  
  $results = $doc3-&gt;getElementsByTagName('test');
  foreach($results as $test){
      //$key=$doc3-&gt;$test;
      if (isset($seen[$test])) {
          unset($doc3-&gt;$test);
      }else{
          $seen[$test]=1;
      }
  }
  
  echo $doc3-&gt;asXML();
  ?&gt;
  </code></pre>