title: PHP array with levels to HTML tree
body: <p>I have an array in php that looks kind of like this:</p>
  
  <pre><code>array[x][0]=item name
  array[x][1]=level
  </code></pre>
  
  <p>so, for example, </p>
  
  <pre><code>array[0][0]="Node 1"
  array[0][1]=0
  array[1][0]="Node 2"
  array[1][1]=0
  array[2][0]="Node 2.1"
  array[2][1]=1
  array[3][0]="Node 2.1.1"
  array[3][1]=2
  array[4][0]="Node 2.2"
  array[4][1]=1
  </code></pre>
  
  <p>I need to turn it into an html ul list, but one that looks like this:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;
          &lt;input type="checkbox" /&gt;&lt;span&gt;Node 1&lt;/span&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;input type="checkbox" /&gt;&lt;span&gt;Node 2&lt;/span&gt;
          &lt;ul&gt;
              &lt;li&gt;
                  &lt;input type="checkbox" /&gt;&lt;span&gt;Node 2.1&lt;/span&gt;
                  &lt;ul&gt;
                      &lt;li&gt;&lt;input type="checkbox"&gt;&lt;span&gt;Node 2.1.1&lt;/span&gt;&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;input type="checkbox" /&gt;&lt;span&gt;Node 2.2&lt;/span&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>The biggest catch is the position of the <code>&lt;/li&gt;</code> tag - it needs to be after the next <code>&lt;ul&gt;</code> tag.  So it's <code>&lt;li&gt;</code> current node <code>&lt;ul&gt;&lt;li&gt;</code> child node <code>&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;</code>.  It's that last <code>&lt;/li&gt;</code> that gets me.</p>
  
  <p>This format is necessary to work with the jquery plugin I'm using (<a href="https://github.com/daredevel/jquery-tree" rel="nofollow noreferrer">https://github.com/daredevel/jquery-tree</a>).</p>
  
  <p>I've seen the suggestions here: <a href="https://stackoverflow.com/questions/31027649/create-html-list-from-array-with-levels">create html list from array with levels</a> but that doesn't work due to the extra <li>.  </p>
  
  <p>I think there's a solution to this using recursion and array slicing, but that seems inefficient.  So far, my attempts at an iterative solution have failed...</p>