Skip to content

Instantly share code, notes, and snippets.

@hakre
Last active December 20, 2015 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakre/c131eb6e5428b9b5a06a to your computer and use it in GitHub Desktop.
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.
<?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