1. Go to this page and download the library: Download bentools/cartesian-product library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bentools / cartesian-product example snippets
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']);
}
print_r(cartesian_product($data)->asArray());
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
)
)