title: PHP convert Array to XML
body: <p>I have an array like so:</p>
  
  <pre><code>[0] =&gt; Array
          (
              [Project] =&gt; Array
                  (
                      [ExternalProjectID] =&gt; 53
                      [ProjectName] =&gt; Doon Creek
                      [Location] =&gt; Array
                          (
                              [Address] =&gt; 123 Fake Street
                              [City] =&gt; Toronto
                              [Province] =&gt; ON
                              [Latitude] =&gt; 43.0000
                              [Longitude] =&gt;  -80.0000
                          )
  
                      [Website] =&gt; http://www.website.com/our-communities.php?newcommunity=53
                      [ContactInformation] =&gt; Array
                          (
                              [ContactPhone] =&gt; 555-5555
                              [ContactEmail] =&gt; email@email.com
                              [SalesOfficeAddress] =&gt; 123 Fake Street
                              [SalesOfficeCity] =&gt; Toronto
                              [SalesOfficeProvince] =&gt; ON
                          )
  
                  )
  
          )
  </code></pre>
  
  <p>Now I am trying to covert this to XML exactly how this looks in the array, like Project should be a node with everything inside, Location should also be a node with everything inside the Location array inside that node, along with ContactInformation.</p>
  
  <p>This is what I have:</p>
  
  <pre><code>$xml = new SimpleXMLElement();
  
  $node = $xml-&gt;addChild('Projects');
  
  array_to_xml($newArray, $node);
  
  echo $xml-&gt;asXML();
  die();
  
  function array_to_xml($array, &amp;$xml) {
      foreach($array as $key =&gt; $value) {
          if(is_array($value)) {
              if(!is_numeric($key)){
                  $subnode = $xml-&gt;addChild("$key");
                  array_to_xml($value, $xml);
              } else {
                  array_to_xml($value, $xml);
              }
          } else {
              $xml-&gt;addChild("$key","$value");
          }
      }
  }
  </code></pre>
  
  <p>but Project, Location and ContactInformation return as  like so:</p>
  
  <pre><code>&lt;Projects&gt;
        &lt;Project /&gt;
        &lt;ExternalProjectID&gt;53&lt;/ExternalProjectID&gt;
        &lt;ProjectName&gt;Doon Creek&lt;/ProjectName&gt;
        &lt;Location /&gt;
        &lt;Address&gt;123 Fake St.&lt;/Address&gt;
        &lt;City&gt;Toronto&lt;/City&gt;
        &lt;Province&gt;ON&lt;/Province&gt;
        &lt;Latitude&gt;43.0000&lt;/Latitude&gt;
        &lt;Longitude&gt;-80.000&lt;/Longitude&gt;
        &lt;Website&gt;http://www.website.com/our-communities.php?newcommunity=53&lt;/Website&gt;
        &lt;ContactInformation /&gt;
        &lt;ContactPhone&gt;555-5555&lt;/ContactPhone&gt;
        &lt;ContactEmail&gt;email@email.com&lt;/ContactEmail&gt;
        &lt;SalesOfficeAddress&gt;123 Fake Street&lt;/SalesOfficeAddress&gt;
        &lt;SalesOfficeCity&gt;Toronto&lt;/SalesOfficeCity&gt;
        &lt;SalesOfficeProvince&gt;ON&lt;/SalesOfficeProvince&gt;
        &lt;Project /&gt;
  &lt;/Projects&gt;
  </code></pre>
  
  <p>My question is how do I fix my XML Output?</p>