title: Converting flat array into an array grouped by categories
body: <p>I've got a database table that looks like this:</p>
  
  <pre><code>uid | group  | category
  1   | group1 | cat1
  2   | group1 | cat2
  3   | group2 | cat3
  4   | group2 | cat4
  5   | group2 | cat5
  6   | group3 | cat6
  7   | group3 | cat7
  </code></pre>
  
  <p>But I need this data in an array, that groups categories by their <code>group</code>.</p>
  
  <p>For example, my array should look like this:</p>
  
  <pre><code>Array
  (
      [group1] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [0] =&gt; 1
                      [1] =&gt; cat1
  
                  )
  
              [1] =&gt; Array
                  (
                      [0] =&gt; 2
                      [1] =&gt; cat2
                  )
  
          )
  
      [group2] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [0] =&gt; 3
                      [1] =&gt; cat3
                  )
  
              [1] =&gt; Array
                  (
                      [0] =&gt; 4
                      [1] =&gt; cat4
                  )
  
              [2] =&gt; Array
                  (
                      [0] =&gt; 5
                      [1] =&gt; cat5
                  )
  
          )
  
      [group3] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [0] =&gt; 6
                      [1] =&gt; cat6
                  )
              [1] =&gt; Array
                  (
                      [0] =&gt; 7
                      [1] =&gt; cat7
                  )
  
          )
  
  )
  </code></pre>
  
  <p>I've written a foreach loop that does just this, but I have a problem.</p>
  
  <p>My problem is that it always leaves out the very last row of the table, and I'm not sure how to fix it. In my mind, the logic dictates that it should always work.</p>
  
  <p>I was thinking that after the loop I could just add the very last row to the new array, but I think that may cause issues if the last row has a different group, and I would rather the solution be built into the foreach loop.</p>
  
  <p>Unfortunately, I am at a loss here. How can I fix my code to include the very last row of the database query?</p>
  
  <p>I would also be interested to see what improvements I can make on my current code, but that may be a better question for codereview.</p>
  
  <p><strong>My loop:</strong></p>
  
  <pre><code>$pass = [];
  foreach($stmt as $key =&gt; $value) {
      if(empty($currentGroup)) $currentGroup = $value['group'];
      if(empty($temp)) $temp = [];
      if($currentGroup != $value['group'] || $key+1 == count($stmt)) {
          $pass[$currentGroup] = $temp;
          $currentGroup = $value['group'];
          $temp = [];
          $temp[] = [$stmt[$key]['uid'], $stmt[$key]['category']];
      } else {
          $temp[] = [$stmt[$key]['uid'], $stmt[$key]['category']];
      }
  }
  </code></pre>