PHP 查找数组的所有(有点)唯一组合 [英] PHP Find All (somewhat) Unique Combinations of an Array

查看:37
本文介绍了PHP 查找数组的所有(有点)唯一组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一整天都在研究 PHP 数组排列/组合问题......但仍然无法弄清楚:/

I've been looking at PHP array permutation / combination questions all day.. and still can't figure it out :/

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

If I have an array like:

20 //key being 0    
20 //key being 1    
22 //key being 2    
24 //key being 3

我需要这样的组合:

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

我目前拥有的代码给了我:

The code I currently have gives me:

20, 22, 24

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

because it doesn't want to repeat 20... but that's what I need!

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

Here is the code I have. it is directly from Php recursion to get all possibilities of strings

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;


}

我一直在玩 ($b == $lastEl) 行,但没有运气

I've been playing around with the ($b == $lastEl) line, with no luck

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

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

我已经看过的问题,与造成内存不足错误的 OR 不同!:

Questions I've already looked at, and are not the same OR that created an out of memory error!:

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

I've tried some of these algorithms with an array of 12 items, and end up running out of memory. However the algorithm that I'm currently using doesn't give me an out of memory error.... BUT.. I need those duplicates!

推荐答案

如果你不介意使用几个全局变量,你可以在 PHP 中做到这一点(翻译自 版本 在 JavaScript 中):

If you don't mind using a couple of global variables, you could do this in PHP (translated from a version in 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));
?>

输出:

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 ) ) 

这篇关于PHP 查找数组的所有(有点)唯一组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆