A PHP port of Python's itertools, built on generators.
Requires PHP 8.4+. All itertools helpers are implemented, plus iter, enumerate, and range.
composer require bolknote/itertoolsuse Itertools\Itertools as it;
foreach (it::islice(it::cycle('ABC'), 10) as $element) {
echo $element; // ABCABCABCA
}Strings are treated as sequences of characters (like Python), alongside arrays and any Traversable.
| Area | Methods |
|---|---|
| Infinite / slicing | count, cycle, repeat, islice |
| Terminating | accumulate, batched, chain, chain_from_iterable, compress, dropwhile, filterfalse, groupby, ifilter, imap, izip, izip_longest, pairwise, starmap, takewhile, tee |
| Combinatoric | product, permutations, combinations, combinations_with_replacement |
| Extras | iter, enumerate, range |
Notes:
izip_longest(...$iterables, $fillvalue)— fill value is the last argument (PHP has no keyword-only params after a variadic).product(...$iterables, $repeat)— if the last argument is anint, it is Python'srepeat.groupbyyields[key, list]pairs (groups are materialized arrays).teeshares a buffer so clones can be consumed independently / interleaved.accumulate(..., $initial = null)— like Python,nullmeans “no initial value”.
composer update
composer test # PHPUnit
composer phpstan # static analysis