-
-
Save hakre/c131eb6e5428b9b5a06a to your computer and use it in GitHub Desktop.
http://stackoverflow.com/questions/17959127/php-array-join-each-sub-array-together-probability NOTE: Does not check for empty input sets which are invalid.
This file contains 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 | |
/** | |
* @link https://gist.github.com/hakre/c131eb6e5428b9b5a06a | |
* @link http://stackoverflow.com/a/18170304/367456 | |
* @link https://eval.in/private/67411a840f2de9 | |
*/ | |
// input | |
$array = [ | |
['a', 'b', 'c'], | |
['e', 'f', 'g'], | |
['h', 'i', 'j', 'k', 'l'], | |
]; | |
/** | |
* Cartesian Product Generator (array-based) | |
*/ | |
function cartesianProduct(array $array) | |
{ | |
// exit early | |
if (!$array) | |
{ | |
return; | |
} | |
// obtain length of array | |
$length = count($array); | |
// obtain length of elements in the array | |
$lengths = array_map('count', $array); | |
// obtain array keys (not visible in question) | |
$arrayKeys = array_keys($array); | |
// obtain array sub-keys (not visible in question) | |
$keys = array_map('array_keys', $array); | |
// define the order (last sub-array first in question) | |
$order = array_keys( | |
array_reverse($arrayKeys, TRUE) | |
); | |
// define counter per each key | |
$counters = array_combine( | |
$arrayKeys, | |
array_fill(0, $length, 0) | |
); | |
// working loop | |
$valid = TRUE; | |
while ($valid) | |
{ | |
// create tuple | |
$values = $ykeys = []; | |
foreach ($array as $key => $sub) | |
{ | |
$values[] = $sub[$keys[$key][$ykeys[] = $counters[$key]]]; | |
} | |
yield $ykeys => $values; | |
// count up | |
foreach ($order as $key) | |
{ | |
if (++$counters[$key] == $lengths[$key]) | |
{ | |
$counters[$key] = 0; | |
continue; | |
} | |
continue 2; | |
} | |
$valid = FALSE; | |
}; | |
} | |
foreach (cartesianProduct($array) as $key => $value) { | |
echo implode(',', $key), ' => ', implode(',', $value), "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment