<?php function generator_chunks(\Generator $generator, $max_chunk_size) { $chunk = []; foreach ($generator as $item) { $chunk[] = $item; if (count($chunk) >= $max_chunk_size) { yield $chunk; $chunk = []; } } if ([] !== $chunk) { // Remaining chunk with fewer items. yield $chunk; } } function generator() { for ($i = 0; $i < 11; ++$i) { yield $i; } } foreach (generator_chunks(generator(), 3) as $chunk) { print json_encode($chunk) . "\n"; }
You have javascript disabled. You will not be able to edit any code.