Laravel - Collections - Method Chunkwhile
The chunkWhile
method breaks the collection into multiple, smaller collections based on the evaluation of the given callback. The $chunk
variable passed to the closure may be used to inspect the previous element:
$collection = collect(str_split('AABBCCCD'));
$chunks = $collection->chunkWhile(function ($value, $key, $chunk) {
return $value === $chunk->last();
});
$chunks->all();
// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]