th3n3rd / cartesian-product
Memory efficient Cartesian Product implementation
v1.0.0
2026-03-16 10:50 UTC
Requires
- php: >=8.0
Requires (Dev)
- devster/ubench: ^1.1
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2026-03-16 10:58:18 UTC
README
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.
Install
Via Composer
$ composer require th3n3rd/cartesian-product
Usage
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();
Testing
$ vendor/bin/phpunit
License
The MIT License (MIT). Please see License File for more information.