PHP查找所有(某种程度上)数组的唯一组合

Kat*_*tie 9 php algorithm recursion combinations permutation

我整天都在看PHP数组排列/组合问题..但仍然无法弄明白:/

如果我有一个像这样的数组:

20 //key being 0    
20 //key being 1    
22 //key being 2    
24 //key being 3
Run Code Online (Sandbox Code Playgroud)

我需要组合如:

20, 20, 22 //keys being 0 1 2    
20, 20, 24 //keys being 0 1 3    
20, 22, 24 //keys being 0 2 3
20, 22, 24 //keys being 1 2 3
Run Code Online (Sandbox Code Playgroud)

我目前的代码给了我:

20, 22, 24
Run Code Online (Sandbox Code Playgroud)

因为它不想重复20 ...但这就是我需要的!

这是我的代码.它直接来自Php递归以获得字符串的所有可能性

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;


}
Run Code Online (Sandbox Code Playgroud)

我一直在玩这($b == $lastEl)条线,没有运气

===============

我已经看过的问题,并且与创建内存不足错误的OR不同!:

我已尝试使用12个项目的数组中的一些算法,并最终耗尽内存.然而,我目前使用的算法并没有给我一个内存不足的错误....但是......我需要那些重复!

גלע*_*רקן 9

如果您不介意使用几个全局变量,可以在PHP中执行此操作(从JavaScript中的版本翻译):

<?PHP
$result = array(); 
$combination = array();

function combinations(array $myArray, $choose) {
  global $result, $combination;

  $n = count($myArray);

  function inner ($start, $choose_, $arr, $n) {
    global $result, $combination;

    if ($choose_ == 0) array_push($result,$combination);
    else for ($i = $start; $i <= $n - $choose_; ++$i) {
           array_push($combination, $arr[$i]);
           inner($i + 1, $choose_ - 1, $arr, $n);
           array_pop($combination);
         }
  }
  inner(0, $choose, $myArray, $n);
  return $result;
}

print_r(combinations(array(20,20,22,24), 3));
?>
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

Array ( [0] => Array ( [0] => 20 
                       [1] => 20 
                       [2] => 22 ) 
        [1] => Array ( [0] => 20 
                       [1] => 20 
                       [2] => 24 ) 
        [2] => Array ( [0] => 20 
                       [1] => 22 
                       [2] => 24 ) 
        [3] => Array ( [0] => 20 
                       [1] => 22 
                       [2] => 24 ) ) 
Run Code Online (Sandbox Code Playgroud)

  • 这里还有一个梨包:http://pear.php.net/package/Math_Combinatorics (2认同)

归档时间:

查看次数:

12710 次

最近记录:

10 年,5 月 前