title: How to convert an array to SimpleXML with legal XML
body: <p>I wanted to post an answer to this tread but it's either closed for answers or I don't have enough points to answer.</p>
  
  <p><a href="https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml">How to convert array to SimpleXML</a></p>
  
  <p>I liked the answer by @Hanmant with the array_to_xml function he wrote. It was almost the answer I needed. I didn't like how it created child elements numbered "item0":</p>
  
  <p>For example:</p>
  
  <p>This is my sample Array:</p>
  
  <pre><code>$array = array();
  $array["exampleOrderId"] = 1097374;
  $array["timeStamp"] = date("Y-m-d", time());
  $array["messages"][] = "Order Received";
  $array["messages"][] = "Order Submitted For Processsing";
  $array["events"][] = "Top Level Event";
  $array["events"][] = "Moving order to processing queue";
  $array["errors"][] = "Order item is not in stock";
  </code></pre>
  
  <p>Initial output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;orderResponse&gt;
     &lt;exampleOrderId&gt;1097374&lt;/exampleOrderId&gt;
     &lt;timeStamp&gt;2018-11-26&lt;/timeStamp&gt;
     &lt;messages&gt;
        &lt;item0&gt;Order Received&lt;/item0&gt;
        &lt;item1&gt;Order Submitted For Processsing&lt;/item1&gt;
     &lt;/messages&gt;
     &lt;events&gt;
        &lt;item0&gt;Top Level Event&lt;/item0&gt;
        &lt;message&gt;
           &lt;item0&gt;Moving order to processing queue&lt;/item0&gt;
           &lt;error&gt;
              &lt;item0&gt;Order item is not in stock&lt;/item0&gt;
           &lt;/error&gt;
        &lt;/message&gt;
     &lt;/events&gt;
  &lt;/orderResponse&gt;
  </code></pre>
  
  <p>There are elements here where messages is an array of message and errors is an array of error. </p>
  
  <p>I made a very small change to his original array_to_xml function so it produces a child element which is the singular version of it's parent:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;orderResponse&gt;
     &lt;exampleOrderId&gt;1097374&lt;/exampleOrderId&gt;
     &lt;timeStamp&gt;2018-11-26&lt;/timeStamp&gt;
     &lt;messages&gt;
        &lt;message&gt;Order Received&lt;/message&gt;
        &lt;message&gt;Order Submitted For Processsing&lt;/message&gt;
     &lt;/messages&gt;
     &lt;events&gt;
        &lt;event&gt;Top Level Event&lt;/event&gt;
        &lt;event&gt;Moving order to processing queue&lt;/event&gt;
     &lt;/events&gt;
     &lt;errors&gt;
        &lt;error&gt;Order item is not in stock&lt;/error&gt;
     &lt;/errors&gt;
  &lt;/orderResponse&gt;
  </code></pre>
  
  <p>This is the revised function:</p>
  
  <pre><code>function array_to_xml( $data, &amp;$xml_data, $element = "") {
      foreach( $data as $key =&gt; $value ) {
          if(! is_numeric($key) ){
              $element = $key;
          } else {
              $element = rtrim($element,'s');
          }
  
          if( is_array($value) ) {
              $subnode = $xml_data-&gt;addChild($element);
              array_to_xml($value, $subnode, $element);
          } else {
              $xml_data-&gt;addChild("$element",htmlspecialchars("$value"));
          }
      }
  }
  </code></pre>