title: PHP: Convert <ul> <li> Tree HTML tag to an array
body: <p>I'm using jsTree and I need to convert this HTML tag tree code <code>&lt;ul&gt; &lt;li&gt;</code> to a PHP array.
  The jsTree HTML tag will be passed to PHP to be parsed and store in a structured tree PHP array(see below for the PHP array structure).</p>
  
  <p><em>Additional question</em>: Is my desired PHP array structure good or you can suggest a good structure? I'm open for suggestions.</p>
  
  <p>Thanks in advance :)</p>
  
  <p>Cheers,
  Mark</p>
  
  <p><strong>jsTree Screenshot</strong>:</p>
  
  <p><img src="https://i.imgur.com/olmxe.png" alt="alt text"></p>
  
  <p><strong>HTML Tree String</strong>:</p>
  
  <pre><code>&lt;ul class="ltr"&gt;
  &lt;li id="phtml_1" class="  open"&gt;
      &lt;a style="" class=" " href="#"&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Folder 1&lt;/a&gt;
      &lt;ul&gt;
      &lt;li class="leaf" id="phtml_2"&gt;
          &lt;a style="" class=" " href="#"&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Child 1.1&lt;/a&gt;
      &lt;/li&gt;
      &lt;li class="open" id="phtml_3"&gt;
          &lt;a style="" class=" " href="#"&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Folder 1.1&lt;/a&gt;
          &lt;ul&gt;
          &lt;li class="leaf last" rel="default"&gt;
              &lt;a href="" style="" class=" "&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Child 1.1.1&lt;/a&gt;
          &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li class="last open" rel="default"&gt;
          &lt;a href="" style="" class=" "&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Folder 1.2&lt;/a&gt;
          &lt;ul&gt;
          &lt;li class="leaf" rel="default"&gt;
              &lt;a href="" style="" class=" "&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Child 1.2.1&lt;/a&gt;
          &lt;/li&gt;
          &lt;li class="leaf last" rel="default"&gt;
              &lt;a href="" style="" class=" "&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Child 1.2.2&lt;/a&gt;
          &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li id="phtml_5" class="file open"&gt;
      &lt;a style="" class=" " href="#"&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Folder 2&lt;/a&gt;
      &lt;ul&gt;
      &lt;li class="leaf" rel="default"&gt;
          &lt;a href="" style="" class=" "&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Child 2.1&lt;/a&gt;
      &lt;/li&gt;
      &lt;li class="leaf last" rel="default"&gt;
          &lt;a href="" style="" class="clicked"&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Child 2.2&lt;/a&gt;
      &lt;/li&gt;
      &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li class="leaf last" rel="default"&gt;
      &lt;a href="" style="" class=" "&gt;&lt;ins&gt;&amp;nbsp;&lt;/ins&gt;Outer Child&lt;/a&gt;
  &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p><strong>PHP Array Structure</strong>:</p>
  
  <pre><code>&lt;?php
  $tree_array = array(
      'Folder 1' =&gt; array(
          'Child 1.1',
          'Folder 1.1' =&gt; array(
              'Child 1.1.1'
          ),
          'Folder 1.2' =&gt; array(
              'Child 1.2.1',
              'Child 1.2.2'
          ),
      ),
      'Folder 2' =&gt; array(
          'Child 2.1',
          'Child 2.2'
      ),
      'Outer Child'
  );
  
  echo '&lt;pre&gt;',print_r($tree_array),'&lt;/pre&gt;';
  ?&gt;
  </code></pre>
  
  <p><strong>PHP print_r Output</strong>:</p>
  
  <pre><code>Array
  (
      [Folder 1] =&gt; Array
          (
              [0] =&gt; Child 1.1
              [Folder 1.1] =&gt; Array
                  (
                      [0] =&gt; Child 1.1.1
                  )
  
              [Folder 1.2] =&gt; Array
                  (
                      [0] =&gt; Child 1.2.1
                      [1] =&gt; Child 1.2.2
                  )
  
          )
  
      [Folder 2] =&gt; Array
          (
              [0] =&gt; Child 2.1
              [1] =&gt; Child 2.2
          )
  
      [0] =&gt; Outer Child
  )
  </code></pre>