title: PHP - Create multidimensional array via a loop based on a count
body: <p>Having some trouble coming up with the right combination of count and foreach for a multidimensional array.</p>
  
  <p>I'm currently doing the following to create an associative array from my db's returned result:</p>
  
  <pre><code>$sql = "SELECT g.id, g.shortname FROM games g ORDER BY g.id ASC";
          $query = $this-&gt;db-&gt;query($sql);
  
          if($query-&gt;num_rows() &gt; 0):
              foreach($query-&gt;result() as $row):
                  $data[$row-&gt;id] = $row-&gt;shortname;
              endforeach;
          return $data;
          else:
              return false;
          endif;
  </code></pre>
  
  <p>This of course produces the following array (which works fine; semi-faux code):</p>
  
  <pre><code>array ( [1] =&gt; CoolGame, [2] =&gt; AnotherGame, [3] =&gt; BetterGame, [4] =&gt; UglyGame)
  </code></pre>
  
  <p>....and so on</p>
  
  <p>But what I want to do is automatically break up results (based on a count var/limiter) into groups via a multidimensional array like so:</p>
  
  <pre><code>array (Group 1 =&gt; 
           array([1] =&gt; CoolGame [2] =&gt; AnotherGame),
         Group 2 =&gt;  
           array([3] =&gt; BetterGame [4] =&gt; UglyGame)
  )
  </code></pre>
  
  <p>So in that example, my <code>$depth_count = 2;</code></p>
  
  <p>In case anyone is interested, I'm doing this to work with auto-generated <code>&lt;optgroup&gt;</code> tags for a multi select via CI's form helper's form_multiselect() function.
  Need some help with tweaking my PHP to allow for this. Thanks!</p>