Skip to content

Instantly share code, notes, and snippets.

@elifiner
Created March 18, 2020 01:09
Show Gist options
  • Save elifiner/834584447b3cc93c19688b11c61f8c9f to your computer and use it in GitHub Desktop.
Save elifiner/834584447b3cc93c19688b11c61f8c9f to your computer and use it in GitHub Desktop.
<?php
$options = [
'a' => [1,2],
'b' => [1,2,3],
];
function cartesian($array)
{
if ($array)
{
$key = end(array_keys($array));
if ($values = array_pop($array)) {
foreach(cartesian($array) as $tuple) {
foreach($values as $value) {
yield $tuple + [$key => $value];
}
}
}
} else {
yield[];
}
}
foreach (cartesian($options) as $tuple) {
echo json_encode($tuple)."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment