Created
March 18, 2020 01:09
-
-
Save elifiner/834584447b3cc93c19688b11c61f8c9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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