title: make a tree of items in selectbox in php
body: <p>I have a code to make a tree in my html select box
  I've filled the array and generate tree in select box 
   like this :</p>
  
  <pre><code>$vertices = array(
      array(
          'id' =&gt; 5,
          'name' =&gt; 'Child3',
          'parent' =&gt; 2,
      ),
      array(
          'id' =&gt; 3,
          'name' =&gt; 'Child1',
          'parent' =&gt; 1,
      ),
      array(
          'id' =&gt; 4,
          'name' =&gt; 'Child2',
          'parent' =&gt; 1,
      ),
      array(
          'id' =&gt; 1,
          'name' =&gt; 'Cat1',
          'parent' =&gt; null,
      ),
      array(
          'id' =&gt; 2,
          'name' =&gt; 'Cat2',
          'parent' =&gt; null,
      ),
  );
  
  /**
   * Generate tree
   */
  $subtrees = $trees = array();
  foreach ($vertices as $vertex) {
      $v = array(
          'id' =&gt; $vertex['id'],
          'name' =&gt; $vertex['name'],
          'children' =&gt; array(),
      );
  
      if (isset($subtrees[$vertex['id']])) {
          $v['children'] = $subtrees[$vertex['id']];
      }
  
      if ($vertex['parent'] === null) {
          $trees[] = $v;
      }
      else if (!isset($subtrees[$vertex['parent']])) {
          $subtrees[$vertex['parent']] = array($v);
      }
      else {
          $subtrees[$vertex['parent']][] = $v;
      }
  }
  unset($subtrees);
  
  /**
   * Generate HTML
   */
  
  function getSubtreeOptions(array $subtreeRoot, $level = 0)
  {
      $html = sprintf('%s&lt;option value="%d"&gt;%s%s&lt;/option&gt;' . PHP_EOL,
          str_repeat("\t", $level + 1),
          $subtreeRoot['id'],
          $level &gt; 0 ? str_repeat(' ', $level) . '--' : null,
          $subtreeRoot['name']);
  
      foreach ($subtreeRoot['children'] as $child) {
          $html .= getSubtreeOptions($child, $level + 1);
      }
  
      return $html;
  }
  
  echo '&lt;select name="stuff"&gt;' . PHP_EOL;
  foreach ($trees as $root) {
      echo getSubtreeOptions($root);
  }
  echo '&lt;/select&gt;';
  </code></pre>
  
  <p>It works fine , but when I want to fill the array by mysql query, It shows nothing in selectbox</p>
  
  <p>this is my db code to fill array : </p>
  
  <pre><code>$res = mysql_query('SELECT id, name, parent FROM groups ORDER BY parent DESC, id') or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);
  $vertices = array();
  while ($vertex = mysql_fetch_assoc($res)) {
      $vertices[] = $vertex;
  }
  </code></pre>
  
  <p>where is the problem?</p>
  
  <p>when i print the array.The output is :</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [id] =&gt; 4
              [name] =&gt; test
              [parent] =&gt; 3
          )
  
      [1] =&gt; Array
          (
              [id] =&gt; 2
              [name] =&gt; test
              [parent] =&gt; 1
          )
  
      [2] =&gt; Array
          (
              [id] =&gt; 3
              [name] =&gt; test
              [parent] =&gt; 1
          )
  
      [3] =&gt; Array
          (
              [id] =&gt; 1
              [name] =&gt; test
              [parent] =&gt; 0
          )
  
      [4] =&gt; Array
          (
              [id] =&gt; 5
              [name] =&gt; test
              [parent] =&gt; 0
          )
  
  )
  </code></pre>