PHP查找阵列的所有(有点)唯一组合

编程入门 行业动态 更新时间:2024-10-27 11:22:48
本文介绍了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我有。它是直接从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!:

  • How我可以在PHP中的所有排列顺序不重复?
  • Permutations - 数字的所有可能的套
  • Combinations,处置和排列在PHP
  • PHP阵列组合
  • 得到一个PHP数组的所有排列?
  • PHP:如何得到一维数组的所有可能的组合?
  • Select从这个数组只有唯一的数组值
  • 得到一个PHP数组的所有排列?
  • PHP:如何得到一维数组的所有可能的组合?
  • Select从这个数组只有唯一的数组值
  • How我可以在PHP中的所有排列顺序不重复?
  • Algorithm返回k个元素的所有组合从n个
  • Find组合(S)元素的数组,其相等于(S)之和给定的数字
  • Combinations,处置和排列在PHP
  • PHP阵列组合
  • Php递归以获得字符串的所有可能性
  • 如何返回数组的排列在PHP
  • Permutations - 数字的所有可能的套
  • 子集和的PHP中的问题与MySQL
  • Find值从阵列过滤掉任何重复对
  • 独特的组合
  • 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中做到这一点(从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查找阵列的所有(有点)唯一组合

本文发布于:2023-10-13 14:31:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   阵列   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!