1

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];
        }   
    }   
}

?>
Propig
  • 31
  • 5

1 Answers1

0

This may not have the same order but this is the same result you are expecting. You just have to add another if condition. I am assuming someone smarter than me here could recreate your function to make it more efficient with lower benchmarks. Otherwise you would have to go for this:

$collect = array();

    function permute($arr, $temp_string, &$collect){
        for($i=0, $iMax = sizeof($arr); $i < $iMax; $i++){
            $arrcopy = $arr;
            $elem = array_splice($arrcopy, $i, 1); 
            $jval = $temp_string." ".$elem[0];
    
            if(!empty($temp_string)){
                $collect [] = explode(" ",$jval);
            }
    
            if(sizeof($arrcopy) > 0){
                permute($arrcopy, $jval, $collect);
            }
        }   
    }

 permute(array('one', 'two', 'tree'), "", $collect);

Would result to:

Array ( [0] => [1] => one [2] => two )
Array ( [0] => [1] => one [2] => two [3] => tree )
Array ( [0] => [1] => one [2] => tree )
Array ( [0] => [1] => one [2] => tree [3] => two )
Array ( [0] => [1] => two [2] => one )
Array ( [0] => [1] => two [2] => one [3] => tree )
Array ( [0] => [1] => two [2] => tree )
Array ( [0] => [1] => two [2] => tree [3] => one )
Array ( [0] => [1] => tree [2] => one )
Array ( [0] => [1] => tree [2] => one [3] => two )
Array ( [0] => [1] => tree [2] => two )
Array ( [0] => [1] => tree [2] => two [3] => one )
Rakushoe
  • 118
  • 11
  • @Propig may I know what is the purpose of this? password cracking and search engine improvement is the only thing I can think of this. – Rakushoe Sep 07 '22 at 15:19
  • 1
    There are countless reasons for generating all permutations of an array. – kmoser Sep 08 '22 at 03:42