title: Creating php arrays from sql queries
body: <p>I'm trying to create an array of arrays so I can build a dynamic menu, but I'm getting lost amongst the code. My output looks like this:</p>
  
  <pre><code>$menu = Array ( 
          [0] =&gt; Array ( 
              [text] =&gt; Home 
              [class] =&gt; 875 
              [link] =&gt; //Home 
              [show_condition] =&gt; TRUE 
              [parent] =&gt; 0 
              ) 
          [1] =&gt; Array ( 
              [text] =&gt; About 
              [class] =&gt; 326 
              [link] =&gt; //About 
              [show_condition] =&gt; TRUE 
              [parent] =&gt; 0 
              ) 
           etc 
           etc 
           etc       
          [339] =&gt; Array ( 
              [text] =&gt; Planner 
              [class] =&gt; 921 
              [link] =&gt; //Planner 
              [show_condition] =&gt; TRUE 
              [parent] =&gt; 45 
              ) 
      ) 
  </code></pre>
  
  <p>And the two functions which should build the menu are:</p>
  
  <pre><code>    function build_menu ( $menu )   {
              $out = '&lt;div class="container4"&gt;' . "\n";
              $out .= '   &lt;div class="menu4"&gt;' . "\n";
              $out .= "\n".'&lt;ul&gt;' . "\n";
  
              for ( $i = 1; $i &lt;= count ( $menu )-1; $i++ )
              {
  
  if ( is_array ( $menu [ $i ] ) ) {//must be by construction but let's keep the errors home
                      if ( $menu [ $i ] [ 'show_condition' ] &amp;&amp; $menu [ $i ] [ 'parent' ] == 0 ) {//are we allowed to see this menu?
                          $out .= '&lt;li class="' . $menu [ $i ] [ 'class' ] . '"&gt;&lt;a href="' . $menu [ $i ] [ 'link' ] . '"&gt;';
                          $out .= $menu [ $i ] [ 'text' ];
                          $out .= '&lt;/a&gt;';
                          $out .= get_childs ( $menu, $i );
                          $out .= '&lt;/li&gt;' . "\n";
                      }
                  }
                  else {
                      die ( sprintf ( 'menu nr %s must be an array', $i ) );
                  }
              }
  
              $out .= '&lt;/ul&gt;'."\n";
              $out .= "\n\t" . '&lt;/div&gt;';
              return $out . "\n\t" . '&lt;/div&gt;';
          }
  
      function get_childs ( $menu, $el_id )   {
              $has_subcats = FALSE;
              $out = '';
              $out .= "\n".'  &lt;ul&gt;' . "\n";
              for ( $i = 1; $i &lt;= count ( $menu )-1; $i++ )
              {
  
                  if ( $menu [ $i ] [ 'show_condition' ] &amp;&amp; $menu [ $i ] [ 'parent' ] == $el_id ) {//are we allowed to see this menu?
                      $has_subcats = TRUE;
                      $add_class = ( get_childs ( $menu, $i ) != FALSE ) ? ' subsubl' : '';
                      $out .= '       &lt;li class="' . $menu [ $i ] [ 'class' ] . $add_class . '"&gt;&lt;a href="' . $menu [ $i ] [ 'link' ] . '"&gt;';
                      $out .= $menu [ $i ] [ 'text' ];
                      $out .= '&lt;/a&gt;';
                      $out .= get_childs ( $menu, $i );
                      $out .= '&lt;/li&gt;' . "\n";
                  }
              }
              $out .= '   &lt;/ul&gt;'."\n";
              return ( $has_subcats ) ? $out : FALSE;
          }
  </code></pre>
  
  <p>But the menu is refusing to show any submenu levels - it only displays top level. Any ideas?</p>
  
  <p>Thanks!</p>