Php recursion to get all possibilities of strings

后端 未结 6 1853
失恋的感觉
失恋的感觉 2020-11-27 07:22

Here is my code to get all possibilities:

$seq[1] = \'d\';
$seq[2] = \'f\';
$seq[3] = \'w\';
$seq[4] = \'s\';

for($i = 1; $i < 5; $i++)
{
    $s[\'length         


        
6条回答
  •  暖寄归人
    2020-11-27 08:12

    One algorithm is here,

    function getCombinations($base,$n){
    
    $baselen = count($base);
    if($baselen == 0){
        return;
    }
        if($n == 1){
            $return = array();
            foreach($base as $b){
                $return[] = array($b);
            }
            return $return;
        }else{
            //get one level lower combinations
            $oneLevelLower = getCombinations($base,$n-1);
    
            //for every one level lower combinations add one element to them that the last element of a combination is preceeded by the element which follows it in base array if there is none, does not add
            $newCombs = array();
    
            foreach($oneLevelLower as $oll){
    
                $lastEl = $oll[$n-2];
                $found = false;
                foreach($base as  $key => $b){
                    if($b == $lastEl){
                        $found = true;
                        continue;
                        //last element found
    
                    }
                    if($found == true){
                            //add to combinations with last element
                            if($key < $baselen){
    
                                $tmp = $oll;
                                $newCombination = array_slice($tmp,0);
                                $newCombination[]=$b;
                                $newCombs[] = array_slice($newCombination,0);
                            }
    
                    }
                }
    
            }
    
        }
    
        return $newCombs;
    
    
    }
    

    I know it is not efficent in any way, but using in small sets should not be a problem

    first base parameter is an array containing elements to be considered when generating combinations.

    for simple usage and output:

    var_dump(getCombinations(array("a","b","c","d"),2));
    

    and output is

    array
      0 => 
        array
          0 => string 'a' (length=1)
          1 => string 'b' (length=1)
      1 => 
        array
          0 => string 'a' (length=1)
          1 => string 'c' (length=1)
      2 => 
        array
          0 => string 'a' (length=1)
          1 => string 'd' (length=1)
      3 => 
        array
          0 => string 'b' (length=1)
          1 => string 'c' (length=1)
      4 => 
        array
          0 => string 'b' (length=1)
          1 => string 'd' (length=1)
      5 => 
        array
          0 => string 'c' (length=1)
          1 => string 'd' (length=1)
    

    To list all subsets of an array, using this combinations algorithm just execute

    $base =array("a","b","c","d");
    
    for($i = 1; $i<=4 ;$i++){
        $comb = getCombinations($base,$i);
    
        foreach($comb as $c){
            echo implode(",",$c)."
    "; } }

    And output is

    a
    b
    c
    d
    a,b
    a,c
    a,d
    b,c
    b,d
    c,d
    a,b,c
    a,b,d
    a,c,d
    b,c,d
    a,b,c,d
    

提交回复
热议问题