Search code examples
phparraysgrouping

Group recurring values from a flat array into their own respective subarrays


I have an array that that I would like convert to a 2d array by grouping identical values together.

Once all elements with the same values have been pushed into the group, I want to use that group for something, then continue the loop and setup the next group, until all content of the original $array has been used.

public function adjust($array){ 
  // $array = [1, 1, 2, 2, 8, 8, 8, 9, 20]
 
  $group = array()

  for ($i = 0; $i < sizeof($array); $i++){
   // code here
   // result should be $group = [1, 1] 
   // use $group for something, reset its contents, 
   // continue loop until group is [2, 2],
   // rinse and repeat
 }    
}

I don't necesarrily need a loop, I just require to be able to slice $array into 5 $groups (for this specific example), to be able to process the data.

Is there an easier way to solve this without a bunch of if/else conditions?


Solution

  • Initialize $chunk to an array containing the first element. Then loop through the rest of the array, comparing the current element to what's in $chunk. If it's different, process $chunk and reset it.

    $chunk = array[$array[0]];
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i] != $chunk[0]) {
            // use $chunk for something
            $chunk = array($array[$i]);
        } else {
            $chunk[] = $array[$i];
        }
    }
    // process the last $chunk here when loop is done