Is there any way I could pull something like this when I call the function:
perm(array("one", "two", "tree"));
that would result to:
Array ( [0] => one [1] => two )
Array ( [0] => one [1] => tree )
Array ( [0] => two [1] => one )
Array ( [0] => two [1] => tree )
Array ( [0] => tree [1] => one )
Array ( [0] => tree [1] => two )
Array ( [0] => one [1] => two [2] => tree )
Array ( [0] => two [1] => one [2] => tree )
Array ( [0] => one [1] => tree [2] => two )
Array ( [0] => tree [1] => one [2] => two )
Array ( [0] => two [1] => tree [2] => one )
Array ( [0] => tree [1] => two [2] => one )
The problem is the array is dynamic and the value could become something like perm(array("one", "two", "tree", "five","8"));
I wanted to generate all the permutation even if the it becomes.
perm(array("one", "two")); or perm(array("two", "tree", "five","8"));
The problem of what I have below is that it starts from 1 permutation instead of 2 permutation above.
</php
function perm($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect []= $temp_string;
for ($i=0, $iMax = sizeof($arr); $i < $iMax; $i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1);
if (sizeof($arrcopy) > 0) {
perm($arrcopy, $temp_string ." " . $elem[0], $collect);
} else {
$collect []= $temp_string. " " . $elem[0];
}
}
}
?>