title: PHP xml to array - how to get rid of empty tags?
body: <p><em>There is the same problem at <a href="https://stackoverflow.com/questions/38308116/xml-to-array-remove-empty-array-php">xml to array - remove empty array php</a>
  Dont know how you handle this. I mean how can i get an answer to a question that is not mine and asked > 2 years ago.
  So im asking here my own question:</em></p>
  
  <p>Simple script:</p>
  
  <pre><code>$xml
      = '&lt;?xml version="1.0"?&gt;
         &lt;Envelope&gt;
             &lt;foo&gt;
                 &lt;bar&gt;
                     &lt;baz&gt;Hello&lt;/baz&gt;
                     &lt;bat/&gt;
                 &lt;/bar&gt; 
             &lt;/foo&gt;
             &lt;foo&gt;
                 &lt;bar&gt;
                     &lt;baz&gt;Hello Again&lt;/baz&gt;
                     &lt;bat&gt;&lt;/bat&gt;
                 &lt;/bar&gt;
             &lt;/foo&gt;
             &lt;foo&gt;
                 &lt;bar&gt;
                     &lt;baz&gt;Hello Again&lt;/baz&gt;
                     &lt;bat&gt; &lt;/bat&gt;
                 &lt;/bar&gt;
             &lt;/foo&gt;
         &lt;/Envelope&gt;';
  
  $xml = new \SimpleXMLElement(
      $xml,
      LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG | LIBXML_NOCDATA
  );
  $array = json_decode(json_encode((array)$xml), true);
  // [
  //     'foo' =&gt; [
  //         0 =&gt; [
  //             'bar' =&gt; [
  //                 'baz' =&gt; 'Hello',
  //                 'bat' =&gt; [], &lt;&lt;-- how to get this to NULL
  //             ],
  //         ],
  //         1 =&gt; [
  //             'bar' =&gt; [
  //                 'baz' =&gt; 'Hello Again',
  //                 'bat' =&gt; [], &lt;&lt;-- how to get this to NULL
  //             ],
  //         ],
  //         2 =&gt; [
  //             'bar' =&gt; [
  //                 'baz' =&gt; 'Hello Again',
  //                 'bat' =&gt; [   &lt;&lt;-- how to get this to NULL
  //                     0 =&gt; ' ',     or at least to value of " " without array
  //                 ],
  //             ],
  //         ],
  //     ],
  // ];
  </code></pre>
  
  <p>As you can see there is an empty <code>&lt;bat/&gt;</code> tag and a whitespace in the last <code>&lt;bat&gt; &lt;/bat&gt;</code> tag.</p>
  
  <p>I would like to get those to <code>null</code> in the array.</p>
  
  <p>I tried the following but this works for the first level only ofc:</p>
  
  <pre><code>$data = (array)$xml;
  foreach ($data as &amp;$item) {
      if (
          $item instanceof \SimpleXMLElement
          and $item-&gt;count() === 0
      ) {
          // is a object(SimpleXMLElement)#1 (0) {}
          $item = null; 
      }
  }
  </code></pre>
  
  <p>I tried and failed doing this recursively. </p>
  
  <p>Also tried <code>RecursiveIteratorIterator</code> but failed.</p>
  
  <p>But there must be a way to get those offset to <code>null</code>. </p>
  
  <p>Anybody done this before? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>Solved</strong>. See <a href="https://stackoverflow.com/a/55733384/3411766">https://stackoverflow.com/a/55733384/3411766</a></p>