-
-
Save fabiocicerchia/4556892 to your computer and use it in GitHub Desktop.
PHP - Generate all the possible combinations among a set of nested arrays.
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
/** | |
* Generate all the possible combinations among a set of nested arrays. | |
* | |
* @param array $data The entrypoint array container. | |
* @param array $all The final container (used internally). | |
* @param array $group The sub container (used internally). | |
* @param mixed $val The value to append (used internally). | |
* @param int $i The key index (used internally). | |
*/ | |
function generate_combinations(array $data, array &$all = array(), array $group = array(), $value = null, $i = 0) | |
{ | |
$keys = array_keys($data); | |
if (isset($value) === true) { | |
array_push($group, $value); | |
} | |
if ($i >= count($data)) { | |
array_push($all, $group); | |
} else { | |
$currentKey = $keys[$i]; | |
$currentElement = $data[$currentKey]; | |
foreach ($currentElement as $val) { | |
generate_combinations($data, $all, $group, $val, $i + 1); | |
} | |
} | |
return $all; | |
} | |
$data = array( | |
array('a', 'b'), | |
array('e', 'f', 'g'), | |
array('w', 'x', 'y', 'z'), | |
); | |
$combos = generate_combinations($data); | |
print_r($combos); |
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
Array | |
( | |
[0] => Array | |
( | |
[0] => a | |
[1] => e | |
[2] => w | |
) | |
[1] => Array | |
( | |
[0] => a | |
[1] => e | |
[2] => x | |
) | |
[2] => Array | |
( | |
[0] => a | |
[1] => e | |
[2] => y | |
) | |
[3] => Array | |
( | |
[0] => a | |
[1] => e | |
[2] => z | |
) | |
[4] => Array | |
( | |
[0] => a | |
[1] => f | |
[2] => w | |
) | |
[5] => Array | |
( | |
[0] => a | |
[1] => f | |
[2] => x | |
) | |
[6] => Array | |
( | |
[0] => a | |
[1] => f | |
[2] => y | |
) | |
[7] => Array | |
( | |
[0] => a | |
[1] => f | |
[2] => z | |
) | |
[8] => Array | |
( | |
[0] => a | |
[1] => g | |
[2] => w | |
) | |
[9] => Array | |
( | |
[0] => a | |
[1] => g | |
[2] => x | |
) | |
[10] => Array | |
( | |
[0] => a | |
[1] => g | |
[2] => y | |
) | |
[11] => Array | |
( | |
[0] => a | |
[1] => g | |
[2] => z | |
) | |
[12] => Array | |
( | |
[0] => b | |
[1] => e | |
[2] => w | |
) | |
[13] => Array | |
( | |
[0] => b | |
[1] => e | |
[2] => x | |
) | |
[14] => Array | |
( | |
[0] => b | |
[1] => e | |
[2] => y | |
) | |
[15] => Array | |
( | |
[0] => b | |
[1] => e | |
[2] => z | |
) | |
[16] => Array | |
( | |
[0] => b | |
[1] => f | |
[2] => w | |
) | |
[17] => Array | |
( | |
[0] => b | |
[1] => f | |
[2] => x | |
) | |
[18] => Array | |
( | |
[0] => b | |
[1] => f | |
[2] => y | |
) | |
[19] => Array | |
( | |
[0] => b | |
[1] => f | |
[2] => z | |
) | |
[20] => Array | |
( | |
[0] => b | |
[1] => g | |
[2] => w | |
) | |
[21] => Array | |
( | |
[0] => b | |
[1] => g | |
[2] => x | |
) | |
[22] => Array | |
( | |
[0] => b | |
[1] => g | |
[2] => y | |
) | |
[23] => Array | |
( | |
[0] => b | |
[1] => g | |
[2] => z | |
) | |
) |
Thanks for saving life. -_-
created an account just to thank you.
you are a lifesaver!
I owe you a beer!
I have problem on processing multiple combination array.
Çok teşekkür ederim harikasın
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've modified function to keep your index key, example :
$options = array(
'a' => array('a1','b1'),
'b' => array('a2','b2')
);
the output will be :
Array
(
[0] => Array
(
[a] => a1
[b] => a2
)
)
/**
*
*/
function generate_combinations(array $data, array &$all = array(), array $group = array(), $value = null, $i = 0,$key = null)
{
$keys = array_keys($data);
if (isset($value) === true) {
$group[$key] = $value;
}
if ($i >= count($data)) {
array_push($all, $group);
} else {
$currentKey = $keys[$i];
$currentElement = $data[$currentKey];
if(count($data[$currentKey]) <= 0) {
generate_combinations($data, $all, $group, null, $i + 1,$currentKey);
} else {
foreach ($currentElement as $val) {
generate_combinations($data, $all, $group, $val, $i + 1,$currentKey);
}
}
}
return $all;
}