th3n3rd/cartesian-product
Memory-efficient Cartesian Product generator for PHP. Uses iterators to yield one tuple at a time, letting you handle very large combinations without big memory usage. Build products via fluent with() calls or CartesianProduct::of(), iterate or toArray().
Memory efficient Cartesian Product implementation.
It uses iterators in order to store only a specific tuple at a time, being able to compute even large combinations without affecting the memory footprint.
Via Composer
$ composer require th3n3rd/cartesian-product
The library can be used as an iterator:
use Nerd\CartesianProduct\CartesianProduct;
$cartesianProduct = CartesianProduct::empty()
->with(['a', 'b', 'c'])
->with(['d', 'e'])
;
// or you can use the `of` static method:
$cartesianProduct = CartesianProduct::of([
['a', 'b', 'c'],
['d', 'e'],
]);
foreach ($cartesianProduct as $index => $product) {
printf("[%s] (%s)\n", $index, implode(',', $product));
}
You can also compute the whole result at once (not recommended for large sets):
$result = $cartesianProduct->toArray();
$ vendor/bin/phpunit
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?