I have an php array that that i would like split to up into smaller chunks using a loop.
Once all elements with the same values have been pushed into the chunk, i want to use that chunk for something, then continue the loop and setup the next chunk, until all content of the original $array has been used.
public function adjust($array){
// $array = [1, 1, 2, 2, 8, 8, 8, 9, 20]
$chunk = array()
for ($i = 0; $i < sizeof($array); $i++){
// code here
// result should be $chunk = [1, 1]
// use $chunk for something, reset its contents,
// continue loop until chunk is [2, 2],
// rinse and repeat
}
}
I dont necesarrily need a loop, i just require to be able to slice $array
into 5 $chunks
(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 clauses ?