title: Recursive DOMDocument from PHP array
body: <p>I have an array that looks like following:</p>
  
  <pre><code>Array
  (
  [0] =&gt; Array
      (
          [name] =&gt; Residential
          [attributes] =&gt; Array
              (
                  [Type] =&gt; Townhouse
              )
  
      )
  
  [1] =&gt; Array
      (
          [name] =&gt; Value
          [value] =&gt; 250000
      )
  
  [2] =&gt; Array
      (
          [name] =&gt; Other
          [value] =&gt; 
      )
  
  [3] =&gt; Array
      (
          [name] =&gt; Price
          [attributes] =&gt; Array
              (
                  [PriceAmount] =&gt; 200000
                  [Contract] =&gt; No
                  [StampDuty] =&gt; 20000
              )
  
          [0] =&gt; Array
              (
                  [name] =&gt; Date
                  [value] =&gt; 2015-09-16
              )
  
      )
  
  [4] =&gt; Array
      (
          [name] =&gt; Income
          [attributes] =&gt; Array
              (
                  [RentalAmount] =&gt; 2000
              )
  
          [0] =&gt; Array
              (
                  [name] =&gt; Period
                  [attributes] =&gt; Array
                      (
                          [Unit] =&gt; Monthly
                      )
  
              )
  
      )
  
  [5] =&gt; Array
      (
          [name] =&gt; Contact
          [attributes] =&gt; Array
              (
                  [Type] =&gt; Owner
              )
  
          [0] =&gt; Array
              (
                  [name] =&gt; Company
                  [attributes] =&gt; Array
                      (
                          [Business] =&gt; ABC Contacts
                      )
  
              )
  
          [1] =&gt; Array
              (
                  [0] =&gt; Array
                      (
                          [name] =&gt; Person
                          [0] =&gt; Array
                              (
                                  [name] =&gt; Title
                                  [attributes] =&gt; Array
                                      (
                                          [Value] =&gt; Mr
                                      )
  
                              )
  
                          [1] =&gt; Array
                              (
                                  [name] =&gt; First
                                  [value] =&gt; Michael
                              )
  
                          [2] =&gt; Array
                              (
                                  [name] =&gt; Surname
                                  [value] =&gt; Jordan
                              )
  
                      )
  
              )
  
          [2] =&gt; Array
              (
                  [name] =&gt; Home
                  [0] =&gt; Array
                      (
                          [name] =&gt; Phone
                          [0] =&gt; Array
                              (
                                  [name] =&gt; Mobile
                                  [value] =&gt; 0414888999
                              )
  
                      )
  
                  [1] =&gt; Array
                      (
                          [name] =&gt; Phone
                          [0] =&gt; Array
                              (
                                  [name] =&gt; Fixed
                                  [attributes] =&gt; Array
                                      (
                                          [CountryCode] =&gt; 
                                          [AreaCode] =&gt; 
                                      )
  
                                  [value] =&gt; 99999999
                              )
  
                      )
              )
  
      )
  )
  </code></pre>
  
  <p>I am using a recursive function to try to generate a DOM in order to get that array in XML:</p>
  
  <pre><code>private function GenerateElements($dom, $data)
  {
      $child = '';
      if (empty($data['name'])) {
  
          if (is_array($data)) {
              foreach ($data as $item) {
                  $child = $this-&gt;GenerateElements($dom, $item);
              }
          }
  
          return $child;
      }
  
      // Create the element
      $element_value = (!empty($data['value'])) ? $data['value'] : null;
      $element = $dom-&gt;createElement("ns5:" . $data['name'], $element_value);
  
      // Add attributes
      if (!empty($data['attributes']) &amp;&amp; is_array($data['attributes'])) {
          foreach ($data['attributes'] as $attribute_key =&gt; $attribute_value) {
              $element-&gt;setAttribute($attribute_key, $attribute_value);
          }
      }
  
      // Any other items in the data array should be child elements
      foreach ($data as $data_key =&gt; $child_data) {
          if (!is_numeric($data_key)) {
              continue;
          }
  
          $child = $this-&gt;GenerateElements($dom, $child_data);
          if ($child) {
              $element-&gt;appendChild($child);
          }
      }
  
      return $element;
  }
  </code></pre>
  
  <p>Called via:</p>
  
  <pre><code>$doc = new DOMDocument();
  $child = $this-&gt;GenerateElements($doc, $arr);
  if ($child)
       $doc-&gt;appendChild($child);
  $xml = $doc-&gt;saveXML();
  </code></pre>
  
  <p>The issue I'm having is that the code is overwriting each element, and the dom I am finishing with, only contains the very last array [5].</p>
  
  <p>I've tried so many things, but this recursive function is the end product so far - please help me tweak it so it doesn't overwrite the elements and all 5 array parts are included.</p>
  
  <p>Edit: Just FYI I have tried to adapt my code from here
  <a href="http://www.viper007bond.com/2011/06/29/easily-create-xml-in-php-using-a-data-array/" rel="nofollow">http://www.viper007bond.com/2011/06/29/easily-create-xml-in-php-using-a-data-array/</a></p>
  
  <p>Surprised his original code doesn't work down further levels.</p>