2017 © Pedro Peláez
 

library cartesian-product

A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.

image

bentools/cartesian-product

A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.

  • Monday, March 26, 2018
  • by bpolaszek
  • Repository
  • 3 Watchers
  • 12 Stars
  • 5,717 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 9 % Grown

The README.md

Latest Stable Version License CI Workflow Coverage Total Downloads, (*1)

Cartesian Product

A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array., (*2)

Usage

require_once __DIR__ . '/vendor/autoload.php';

use function BenTools\CartesianProduct\cartesian_product;

$data = [
    'hair' => [
        'blond',
        'black'
    ],
    'eyes' => [
        'blue',
        'green',
        function (array $combination) { // You can use closures to dynamically generate possibilities
            if ('black' === $combination['hair']) { // Then you have access to the current combination being built
                return 'brown';
            }
            return 'grey';
        }
    ]
];

foreach (cartesian_product($data) as $combination) {
    printf('Hair: %s - Eyes: %s' . PHP_EOL, $combination['hair'], $combination['eyes']);
}

Output:, (*3)

Hair: blond - Eyes: blue
Hair: blond - Eyes: green
Hair: blond - Eyes: grey
Hair: black - Eyes: blue
Hair: black - Eyes: green
Hair: black - Eyes: brown

Array output

Instead of using foreach you can dump all possibilities into an array., (*4)

print_r(cartesian_product($data)->asArray());

Output:, (*5)

Array
(
    [0] => Array
        (
            [hair] => blond
            [eyes] => blue
        )

    [1] => Array
        (
            [hair] => blond
            [eyes] => green
        )

    [2] => Array
        (
            [hair] => blond
            [eyes] => grey
        )

    [3] => Array
        (
            [hair] => black
            [eyes] => blue
        )

    [4] => Array
        (
            [hair] => black
            [eyes] => green
        )

    [5] => Array
        (
            [hair] => black
            [eyes] => brown
        )

)

Combinations count

You can simply count how many combinations your data produce:, (*6)

require_once __DIR__ . '/vendor/autoload.php';

use function BenTools\CartesianProduct\cartesian_product;

$data = [
    'hair' => [
        'blond',
        'red',
    ],
    'eyes' => [
        'blue',
        'green',
        'brown',
    ],
    'gender' => [
        'male',
        'female',
    ]
];
var_dump(count(cartesian_product($data))); // 2 * 3 * 2 = 12

Installation

PHP 7.4+ is required., (*7)

composer require bentools/cartesian-product

Performance test

The following example was executed on my Core i7 personnal computer with 8GB RAM., (*8)

require_once __DIR__ . '/vendor/autoload.php';
use function BenTools\CartesianProduct\cartesian_product;

$data = array_fill(0, 10, array_fill(0, 5, 'foo'));

$start = microtime(true);
foreach (cartesian_product($data) as $c => $combination) {
    continue;
}
$end = microtime(true);

printf(
    'Generated %d combinations in %ss - Memory usage: %sMB / Peak usage: %sMB',
    ++$c,
    round($end - $start, 3),
    round(memory_get_usage() / 1024 / 1024),
    round(memory_get_peak_usage() / 1024 / 1024)
);

Output:, (*9)

Generated 9765625 combinations in 1.61s - Memory usage: 0MB / Peak usage: 1MB, (*10)

Unit tests

./vendor/bin/phpunit

Other implementations

th3n3rd/cartesian-product, (*11)

patchranger/cartesian-iterator, (*12)

Benchmark, (*13)

See also

bentools/string-combinations, (*14)

bentools/iterable-functions, (*15)

Credits

Titus on StackOverflow - you really rock., (*16)

The Versions

26/03 2018

dev-master

9999999-dev

A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

23/06 2017

1.2

1.2.0.0

A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

07/06 2017

1.1

1.1.0.0

A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

30/05 2017

1.0.1

1.0.1.0

A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

30/05 2017

1.0

1.0.0.0

A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires