I made some slight modifications to the original code in order to make it use the amount of keys instead of the array values, and then I added a second function to allow a multi-dimensional array to be counted as well.
<?php
function everyCombination($array) {
$newArray = array();
for($keyCount = 1; $keyCount <= count($array); $keyCount++){
$newArray[] = $keyCount;
}
$arrayCount = count($newArray);
$maxCombinations = pow($arrayCount, $arrayCount);
$returnArray = array();
$conversionArray = array();
foreach ($newArray as $key => $value) {
$conversionArray[base_convert($key, 10, $arrayCount)] = $value;
}
for ($i = 0; $i < $maxCombinations; $i++) {
$combination = base_convert($i, 10, $arrayCount);
$combination = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT);
$returnArray[] = strtr($combination, $conversionArray);
}
return $returnArray;
}
function getCombos($array){
if(is_array($array[key($array)])){
$return = array();
foreach($array as $subArray){
$return[] = everyCombination($subArray);
}
}else{
$return = everyCombination($array);
}
return $return;
}
$test = array(53,22,1233,45);
echo '<pre>';
print_r(getCombos($test));
echo '</pre>';
All credit for function and usage goes to https://stackoverflow.com/a/14022357/2285345
3
solved PHP multidimensional array keys combinations/combinatorics [closed]