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

查看:127
本文介绍了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

我需要这样的组合:

I need combinations like:

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

在code我目前有给我:

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!

下面是code我有。它是直接从<一个href="http://stackoverflow.com/questions/4279722/php-recursion-to-get-all-possibilities-of-strings/8880362#8880362">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)行,没有运气

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

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

问题我已经看了,并且是不一样的,或者创建的内存不足的错误:!

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

  • <一个href="http://stackoverflow.com/questions/12238675/how-can-i-get-all-permutations-in-php-without-sequential-duplicates">How我可以在PHP中的所有排列顺序不重复?
  • <一个href="http://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers">Permutations - 数字的所有可能的套
  • <一个href="http://stackoverflow.com/questions/1679605/combinations-dispositions-and-permutations-in-php">Combinations,处置和排列在PHP
  • PHP阵列组合
  • 得到一个PHP数组的所有排列?
  • <一个href="http://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array">PHP:如何得到一维数组的所有可能的组合?
  • <一个href="http://stackoverflow.com/questions/11340450/select-only-unique-array-values-from-this-array">Select从这个数组只有唯一的数组值
  • 得到一个PHP数组的所有排列?
  • <一个href="http://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array">PHP:如何得到一维数组的所有可能的组合?
  • <一个href="http://stackoverflow.com/questions/11340450/select-only-unique-array-values-from-this-array">Select从这个数组只有唯一的数组值
  • <一个href="http://stackoverflow.com/questions/12238675/how-can-i-get-all-permutations-in-php-without-sequential-duplicates">How我可以在PHP中的所有排列顺序不重复?
  • <一个href="http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n">Algorithm返回k个元素的所有组合从n个
  • <一个href="http://stackoverflow.com/questions/12837431/find-combinations-sum-of-elements-in-array-whose-sum-equal-to-a-given-number">Find组合(S)元素的数组,其相等于(S)之和给定的数字
  • <一个href="http://stackoverflow.com/questions/1679605/combinations-dispositions-and-permutations-in-php">Combinations,处置和排列在PHP
  • PHP阵列组合
  • <一个href="http://stackoverflow.com/questions/4279722/php-recursion-to-get-all-possibilities-of-strings/8880362#8880362">Php递归以获得字符串的所有可能性
  • 如何返回数组的排列在PHP <? / A>
  • <一个href="http://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers">Permutations - 数字的所有可能的套
  • 子集和的PHP中的问题与MySQL
  • <一个href="http://stackoverflow.com/questions/9088963/find-unique-combinations-of-values-from-arrays-filtering-out-any-duplicate-pairs">Find值从阵列过滤掉任何重复对
  • 独特的组合
  • <一个href="http://stackoverflow.com/questions/9217839/finding-all-the-unique-permutations-of-the-string">Finding一个字符串的所有独特的排列,而不会产生重复
  • 生成所有独特的排列
  • 子集之和k个整数?
  • How can I get all permutations in PHP without sequential duplicates?
  • Permutations - all possible sets of numbers
  • Combinations, Dispositions and Permutations in PHP
  • PHP array combinations
  • Get all permutations of a PHP array?
  • PHP: How to get all possible combinations of 1D array?
  • Select only unique array values from this array
  • Get all permutations of a PHP array?
  • PHP: How to get all possible combinations of 1D array?
  • Select only unique array values from this array
  • How can I get all permutations in PHP without sequential duplicates?
  • Algorithm to return all combinations of k elements from n
  • Find combination(s) sum of element(s) in array whose sum equal to a given number
  • Combinations, Dispositions and Permutations in PHP
  • PHP array combinations
  • Php recursion to get all possibilities of strings
  • How to return permutations of an array in PHP?
  • Permutations - all possible sets of numbers
  • Subset-sum problem in PHP with MySQL
  • Find unique combinations of values from arrays filtering out any duplicate pairs
  • Finding all the unique permutations of a string without generating duplicates
  • Generate all unique permutations
  • Subset sum for exactly k integers?

我已经尝试了一些这些算法与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中做到这一点(从<一个翻译href="http://stackoverflow.com/questions/4061080/output-each-combination-of-an-array-of-numbers-with-javascript">version在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));
?>

OUTPUT:

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

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

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