title: how to group array based on a value from within that array - php (laravel)
body: <p>I have a query from eloquent which I transform into an array. My goal is to group multiples of members into their respective group based on their group id, which each member holds. So essentially I would need a three dimensional array, with the first array holding the group ids, the second holding an array of each member, and the third holding that members information i.e.</p>
  
  <pre><code>Array(
  
      [1] =&gt; array(
                    array( [member_id] =&gt; 3
                           [type] =&gt; human
                           [group_id] =&gt; 1
                         ),
                    array( [member_id] =&gt; 4
                           [type] =&gt; alien
                           [group_id] =&gt; 1
                         ), 
                  )
      [2] =&gt; array(
                    array( [member_id] =&gt; 9
                           [type] =&gt; human
                           [group_id] =&gt; 2
                         ),
                    array( [member_id] =&gt; 10
                           [type] =&gt; alien
                           [group_id] =&gt; 2
                         ), 
                  )
  )
  </code></pre>
  
  <p>I've tried a few things, the one that has gotten me closest to this goal is the following:</p>
  
  <p>Sorting them by their group gives me a two dimensional array with the members being returned in ascending order, like so:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [member_id] =&gt; 7
              [type] =&gt; human
              [group_id] =&gt; 1
          )
  
      [1] =&gt; Array
          (
              [customer_id] =&gt; 5
              [type] =&gt; alien
              [group_id] =&gt; 1
          )
  
      [2] =&gt; Array
          (
              [customer_id] =&gt; 8
              [type] =&gt; alien
              [group_id] =&gt; 2
          )
  //there is no group #3, so next group is 4
      [3] =&gt; Array
          (
              [customer_id] =&gt; 3
              [type] =&gt; human
              [group_id] =&gt; 4
          )
  )
  </code></pre>
  
  <p>But the keys are not based on group ids, they're just assigned by the function.</p>
  
  <p>To make the first arrays keys actual group ids, I did the following:</p>
  
  <pre><code>        $sorted = array();
          $i=0;
          while($i &lt;= count($members))
          {
              foreach($members as $member)
              {
                  if($member['group_id'] == $i)
                  {
                      $sorted[$i] = $member;
                  }
              }
              echo $i;
              $i++;
  
          }
  </code></pre>
  
  <p>But this gave me only the first member for each group and not any more.</p>
  
  <p>SOLUTION:
  I hope it's still okay to post the answer in my question, it just came to me while I was writing out this question lol...</p>
  
  <p>I wasn't actually creating another array within <code>$sorted[]</code> in my if condition, so that's why it didn't append the other members, only one. I added a pair of brackets:</p>
  
  <pre><code>$sorted[$i][] = $member;
  </code></pre>
  
  <p>and it works :). Hope it helps someone in the future.</p>