title: PHP Create Sub Arrays from Master Array based on keys
body: <p>This pertains to PHP.
  I have an array of arrays that is a recordset from a db query:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonSendTracking
              [2] =&gt; 
              [3] =&gt; 1
              [4] =&gt; IN
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 7
              [7] =&gt; 5
          )
  
      [1] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonGetOrdersAndMove
              [2] =&gt; 
              [3] =&gt; 4
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 6
              [7] =&gt; 4
          )
  
      [2] =&gt; Array
          (
              [0] =&gt; Test
              [1] =&gt; RedirectTest1
              [2] =&gt; data=data1&amp;data2=data2&amp;testvar=testvariable
              [3] =&gt; 4
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 19
              [7] =&gt; 17
          )
  
      [3] =&gt; Array
          (
              [0] =&gt; Test
              [1] =&gt; RedirectTest2
              [2] =&gt; data=value&amp;data2=value2&amp;testvar=value3
              [3] =&gt; 4
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 19
              [7] =&gt; 25
          )
  
      [4] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonPushInventory
              [2] =&gt; 
              [3] =&gt; 15
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 27
              [7] =&gt; 26
          )
  
      [5] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; CalculateFloorCeiling
              [2] =&gt; 
              [3] =&gt; 15
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 27
              [7] =&gt; 27
          )
  
      [6] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonSubmitPricingXML
              [2] =&gt; 
              [3] =&gt; 15
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 27
              [7] =&gt; 28
          )
  
      [7] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonSubmitPricingXMLResetBusinessAndFlipEm
              [2] =&gt; 
              [3] =&gt; 1440
              [4] =&gt; OUT
              [5] =&gt; 
              [6] =&gt; 8
              [7] =&gt; 6
          )
  
  )
  </code></pre>
  
  <p>the Value in the 7th position (6) of the sub-array represents a "group" that I want to break out into individual arrays.</p>
  
  <p>So I want the above to become one array of items not in any group and then a new array for each group like this:</p>
  
  <p><strong>Array1 - All Items not in a group</strong></p>
  
  <pre><code>Array1
  (
      [0] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonSendTracking
              [2] =&gt; 
              [3] =&gt; 1
              [4] =&gt; IN
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 7
              [7] =&gt; 5
          )
  
      [1] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonGetOrdersAndMove
              [2] =&gt; 
              [3] =&gt; 4
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 6
              [7] =&gt; 4
          )
      [2] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonSubmitPricingXMLResetBusinessAndFlipEm
              [2] =&gt; 
              [3] =&gt; 1440
              [4] =&gt; OUT
              [5] =&gt; 
              [6] =&gt; 8
              [7] =&gt; 6
          )
  )
  </code></pre>
  
  <p><strong>Array2 - 1st group</strong></p>
  
  <pre><code>Array2(
   [0] =&gt; Array
          (
              [0] =&gt; Test
              [1] =&gt; RedirectTest1
              [2] =&gt; data=data1&amp;data2=data2&amp;testvar=testvariable
              [3] =&gt; 4
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 19
              [7] =&gt; 17
          )
  
      [1] =&gt; Array
          (
              [0] =&gt; Test
              [1] =&gt; RedirectTest2
              [2] =&gt; data=value&amp;data2=value2&amp;testvar=value3
              [3] =&gt; 4
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 19
              [7] =&gt; 25
          )
  )
  </code></pre>
  
  <p><strong>Array3 - 2nd group</strong></p>
  
  <pre><code>Array3(
      [0] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonPushInventory
              [2] =&gt; 
              [3] =&gt; 15
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 27
              [7] =&gt; 26
          )
  
      [1] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; CalculateFloorCeiling
              [2] =&gt; 
              [3] =&gt; 15
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 27
              [7] =&gt; 27
          )
  
      [2] =&gt; Array
          (
              [0] =&gt; Amazon
              [1] =&gt; AmazonSubmitPricingXML
              [2] =&gt; 
              [3] =&gt; 15
              [4] =&gt; ALL
              [5] =&gt; 2020-01-07 11:32:18
              [6] =&gt; 27
              [7] =&gt; 28
          )
  )
  </code></pre>
  
  <p>Im just not sure what array functions would be best used to accomplish this. I have tried getting the count of each group using the following.
  I have a loop that steps through the master array (array of arrays call $rs) and while in the loop I pull out the 7th position into a variable called $HeaderRecNbr and then do this:</p>
  
  <pre><code>$GroupCount=array_count_values(array_column($rs, 6))[$HeaderRecNbr];
  </code></pre>
  
  <p>but Im not sure where to go from here.</p>