我一整天都在研究 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!:
- 如何获取所有排列PHP 没有连续重复?
- 排列 - 所有可能的数字集
- PHP 中的组合、排列和排列
- PHP 数组组合
- 获取 PHP 数组的所有排列?
- PHP:如何获得所有可能的组合一维数组?
- 仅从此数组中选择唯一的数组值
- 获取 PHP 数组的所有排列?
- PHP:如何获得所有可能的组合一维数组?
- 仅从此数组中选择唯一的数组值
- 如何获取所有排列PHP 没有连续重复?
- 返回k个元素的所有组合的算法无
- 查找数组中元素的总和等于给定数字的组合
- PHP 中的组合、排列和排列
- PHP 数组组合
- PHP 递归获取字符串的所有可能性
- 如何在 PHP 中返回数组的排列?
- 排列 - 所有可能的数字集
- PHP 与 MySQL 中的子集求和问题
- 查找值的唯一组合从数组中过滤掉任何重复的对
- 查找字符串的所有唯一排列而不生成重复项
- 生成所有唯一排列
- 恰好 k 个整数的子集和?
我已经用 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 查找数组的所有(有点)唯一组合
发布评论