title: Passing PHP associative arrays to and from XML
body: <p>Is there an easy way to marshal a PHP associative array to and from XML? For example, I have the following array:</p>
  
  <pre><code>$items = array("1", "2",
      array(
          "item3.1" =&gt; "3.1",
          "item3.2" =&gt; "3.2"
          "isawesome" =&gt; true
      )
  );
  </code></pre>
  
  <p>How would I turn it into something similar to the following XML in as few lines as possible, then back again?</p>
  
  <pre><code>&lt;items&gt;
      &lt;item&gt;1&lt;/item&gt;
      &lt;item&gt;2&lt;/item&gt;
      &lt;item&gt;
          &lt;item3_1&gt;3.1&lt;/item3_1&gt;
          &lt;item3_2&gt;3.2&lt;/item3_2&gt;
          &lt;isawesome&gt;true&lt;/isawesome&gt;
      &lt;/item&gt;
  &lt;/items&gt;
  </code></pre>
  
  <p>I don't really care if I have to change the array structure a bit or if the XML that comes out is different to the above example. I've been trying to work with PHP's <a href="http://php.net/manual/en/book.xmlreader.php" rel="noreferrer">XMLReader</a> and <a href="http://php.net/manual/en/book.xmlwriter.php" rel="noreferrer">XMLWriter</a>, but the documentation is so poor and the code I've produced as a consequence looks nothing like what I feel it should look like:</p>
  
  <pre><code>$xml = SomeXMLWriter::writeArrayToXml($items);
  $array = SomeXMLWriter::writeXmlToArray($xml);
  </code></pre>
  
  <p>Does it really have to be any harder than that to get a basic, raw XML dump of a PHP array without writing my own custom class?</p>
  
  <p>I try to avoid PEAR. In addition to the configuration headaches I've had with it, I've never stuck with any of the packages I've ever used from it.</p>