title: How to recursively build a <select> with unknown tree depth
body: <p>I have a MySQL table with a tree data structure. The fields are <code>_id</code>, <code>name</code> and <code>parentId</code>. When the record hasn't a parent, <code>parentId</code> defaults as 0. This way I can build an array and then recursively print each record.</p>
  
  <p>The builded array looks like this:</p>
  
  <pre><code>Array
  (
      [1] =&gt; Array
          (
              [parentId] =&gt; 0
              [name] =&gt; Countries
              [_id] =&gt; 1
              [children] =&gt; Array
                  (
                      [2] =&gt; Array
                          (
                              [parentId] =&gt; 1
                              [name] =&gt; America
                              [_id] =&gt; 2
                              [children] =&gt; Array
                                  (
                                      [3] =&gt; Array
                                          (
                                              [parentId] =&gt; 2
                                              [name] =&gt; Canada
                                              [_id] =&gt; 3
                                              [children] =&gt; Array
                                                  (
                                                      [4] =&gt; Array
                                                          (
                                                              [parentId] =&gt; 3
                                                              [name] =&gt; Ottawa
                                                              [_id] =&gt; 4
                                                          )
  
                                                  )
  
                                          )
  
                                  )
  
                          )
  
                      [5] =&gt; Array
                          (
                              [parentId] =&gt; 1
                              [name] =&gt; Asia
                              [_id] =&gt; 5
                          )
  
                      [6] =&gt; Array
                          (
                              [parentId] =&gt; 1
                              [name] =&gt; Europe
                              [_id] =&gt; 6
                              [children] =&gt; Array
                                  (
                                      [7] =&gt; Array
                                          (
                                              [parentId] =&gt; 6
                                              [name] =&gt; Italy
                                              [_id] =&gt; 7
                                          )
  
                                      [11] =&gt; Array
                                          (
                                              [parentId] =&gt; 6
                                              [name] =&gt; Germany
                                              [_id] =&gt; 11
                                          )
  
                                      [12] =&gt; Array
                                          (
                                              [parentId] =&gt; 6
                                              [name] =&gt; France
                                              [_id] =&gt; 12
                                          )
  
                                  )
  
                          )
  
                      [8] =&gt; Array
                          (
                              [parentId] =&gt; 1
                              [name] =&gt; Oceania
                              [_id] =&gt; 8
                          )
  
                  )
  
           )
  
   )
  </code></pre>
  
  <p>Printing an unordered list <code>&lt;ul&gt;</code> is very simple with recursion. Here's the function I use:</p>
  
  <pre><code>function toUL ($arr) {
  
      $html = '&lt;ul&gt;' . PHP_EOL;
  
      foreach ( $arr as $v ) {
  
          $html.= '&lt;li&gt;' . $v['name'] . '&lt;/li&gt;' . PHP_EOL;
  
          if ( array_key_exists('children', $v) ) {
              $html.= toUL($v['children']);
          }
  
      }
  
      $html.= '&lt;/ul&gt;' . PHP_EOL;
  
      return $html;
  }
  </code></pre>
  
  <p>But I'm stuck at printing a <code>&lt;select&gt;</code> in a tree-structured way:</p>
  
  <pre><code>Countries
  -- America
  ---- Canada
  ------ Ottawa
  -- Asia
  -- Europe
  ---- Italy
  ---- Germany
  ---- France
  -- Oceania
  </code></pre>
  
  <p>I thought to print <code>--</code> as many times as the element's depth, but I don't know how to calculate the depth.</p>
  
  <p>My question is: is it possible to build a <code>&lt;select&gt;</code> without knowing the depth?</p>
  
  <p>Thank you in advance.</p>