title: php simpleXMLElement to array: null value
body: <p>I've got following XML:</p>
  
  <pre><code>&lt;account&gt;
      &lt;id&gt;123&lt;/id&gt;
      &lt;email&gt;&lt;/email&gt;
      &lt;status&gt;ACTIVE&lt;/status&gt;
  &lt;/account&gt;
  </code></pre>
  
  <p>I want to have it as an array variable. Therefore I read it with <code>$xml = simplexml_load_file()</code>. The simplest way to convert simpleXMLElement to an associative array I know is to grind it with: <code>json_decode(json_encode((array) $xml),1);</code></p>
  
  <p>The problem is that I don't want to get the <code>email</code> key as an empty array, but rather as a <code>NULL</code> value. As SimpleXMLElement, it looks like:</p>
  
  <pre><code>public 'email' =&gt; 
      object(SimpleXMLElement)[205]
  </code></pre>
  
  <p>whereas in array it looks like:</p>
  
  <pre><code>'email' =&gt; 
      array (size=0)
        empty
  </code></pre>
  
  <p>I'd like to get:</p>
  
  <pre><code>'email' =&gt; NULL
  </code></pre>
  
  <p>The only way to achieve this I thought of is iterate through all elements and replace empty array with null value. The problem is that my XML is way bigger (above is just to explain the problem) and I'd have to iterate a lot of XML elements (and this would be manual work - I'm looking for something automatic). Maybe I'm missing some options in one of the functions... or maybe there's another trick to do this?</p>