th3n3rd/cartesian-product

Memory efficient Cartesian Product implementation

Maintainers

Package info

github.com/th3n3rd/cartesian-product

pkg:composer/th3n3rd/cartesian-product

Statistics

Installs: 102 419

Dependents: 3

Suggesters: 0

Stars: 13

Open Issues: 3

v1.0.0 2026-03-16 10:50 UTC

This package is auto-updated.

Last update: 2026-03-16 10:58:18 UTC


README

Latest Version Build Status Software License Total Downloads

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.