title: Array "tree" creation from db table
body: <p>My php function </p>
  
  <pre><code>function generateMenu($parent, $level, $menu, $utype) {
      global $db;
      $tree = array();
      $stmt = $db-&gt;prepare("select id, parent, name FROM navigation WHERE menu=? AND user_type=?") or die($db-&gt;error);
      $stmt-&gt;bind_param("ii", $menu, $utype) or die($stmt-&gt;error);
      $stmt-&gt;execute() or die($stmt-&gt;error);
      $stmt-&gt;store_result();
      $meta = $stmt-&gt;result_metadata();
      $stmt-&gt;bind_result($id, $parent, $name);
      while ($stmt-&gt;fetch()) {
          $arr[$id] = array(
              'name' =&gt; $name, 
              'parent' =&gt; $parent
          );
          if (!array_key_exists($parent,$arr) and $parent != 0) {
              $arr[$parent][$id] = $id;
          }
      }
      $stmt-&gt;close();
  }
  </code></pre>
  
  <p>generates following array from db table. <code>[1], [2]</code> ... - are ids of <code>li</code> item</p>
  
  <pre><code>Array (
      [1] =&gt; Array ( 
          [name] =&gt; Parent1
          [parent] =&gt; 0
      ) 
      [2] =&gt; Array ( 
          [name] =&gt; Parent2
          [parent] =&gt; 0 
      )
      [3] =&gt; Array (
          [name] =&gt; Parent3 
          [parent] =&gt; 0 
      )
      [4] =&gt; Array ( 
          [name] =&gt; Child1 of P1
          [parent] =&gt; 1
      ) 
      [5] =&gt; Array (
          [name] =&gt; Child2 of P1
          [parent] =&gt; 1
      ) 
  )
  </code></pre>
  
  <p>What I want to do is to create the menu like that</p>
  
  <pre><code>&lt;ul&gt;
    &lt;li&gt;&lt;a href="?page=1"&gt;Parent1&lt;/a&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;a href="?page=4"&gt;Child1 of P1&lt;/a&gt;&lt;/li&gt;
          ...
  </code></pre>
  
  <p>Second function is for generating menu from this array. But I know that before sending this array into second function I need to convert it into multidimensional tree array. I can't figure out how to do it.</p>
  
  <p>Here is second function</p>
  
  <pre><code>function olLiTree($tree) {
      $out = '&lt;ul&gt;';
  
      foreach($tree as $key =&gt; $value) {
          $out.= '&lt;li&gt;';
  
          if (is_array($value)) {
              $out.= $key . olLiTree($value);
          } else {
              $out.= $value;
          }
  
          $out.= '&lt;/li&gt;';
      }
  
      $out.= '&lt;/ul&gt;';
  
      return $out;
  }
  </code></pre>
  
  <p>Db structure</p>
  
  <p><img src="https://i.stack.imgur.com/oU2Jr.png" alt="http://img338.imageshack.us/img338/4669/6df2a8e6c7684c8aaa82ddf.png"></p>