Hey there I’ve made a recursive permutation function for class, but I output is less than favorable.
http://codepad.org/DOaMP9oc
function permute($arr) {
$out = array();
if (count($arr) > 1) {
$i = 0;
foreach($arr as $r => $c) {
$n = $arr;
unset($n[$r]);
$out[$c] = permute($n);
}
}
else
return array_shift($arr);
return $out;
}
If input is array(1,2,3,4,5), Output is:
Array
(
[1] => Array
(
[2] => Array
(
[3] => Array
(
[4] => 5
[5] => 4
)
[4] => Array
(
[3] => 5
[5] => 3
)
[5] => Array
(
[3] => 4
[4] => 3
)
)
ETC......................
This is all Correct, you can read it like this key.key.key.key.value or 12345,12354,12435
Currently, to convert this output to something readable, I’m using this ugly block of code:
http://codepad.org/qyWcRBCl
foreach($out as $k => $a)
foreach($a as $l => $b)
foreach ($b as $m => $c)
foreach ($c as $n => $d)
echo $k.$l.$m.$n.$d.'<br>';
I opted to use the following function:
However, I’d still like to do this in a singular recursive function.