从给定数组PHP - [R元素的所有组合 [英] All combinations of r elements from given array php

查看:110
本文介绍了从给定数组PHP - [R元素的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定的阵列,如以下

$array = ('1', '2', '3', '4', '5', '6', '7');

我正在寻找以产生所有可能的组合的方法中,与各组合R必须元素的最小数目。 (例如,如果R = 5那么它将返回一个包含至少5个元素的所有可能的组合)

I'm looking for a method to generate all possible combinations, with a minimum number of elements required in each combination r. (eg if r = 5 then it will return all possible combinations containing at least 5 elements)

推荐答案

A 组合可pssed为

N C <子>研究 = N! /(R - (N - R)!)

nCr = n! / (r! - (n - r)!)

首先,我们确定 $ N 作为数组中元素的个数。和 $ R 是每个组合元素的最小数量。

First, we determine $n as the number of elements in the array. And $r is the minimum number of elements in each combination.

$a = ['1', '2', '3', '4', '5', '6', '7'];  // the array of elements we are interested in

// Determine the `n` and `r` in nCr = n! / (r! * (n-r)!)
$r = 5;
$n = count($a);

接下来,我们确定 $最大为可重新由psented $ P $的最大数量 $ N 二进制数字。也就是说,如果 $ N = 3 ,那么 $最大=(111) 2 = 7 。要做到这一点,我们首先创建一个空字符串 $ maxBinary 并添加 $ N 的数1 s到它。然后,我们将其转换为十进制,并存储它 $最大

Next, we determine $max as the maximum number that can be represented by $n binary digits. That is, if $n = 3, then $max = (111)2= 7. To do this, we first create a empty string $maxBinary and add $n number of 1s to it. We then convert it to decimal, and store it in $max.

$maxBinary = "";
for ($i = 0; $i < $n; $i++)
{
  $maxBinary .= "1";
}
$max = bindec($maxBinary);  // convert it into a decimal value, so that we can use it in the following for loop

然后,我们列出了从 0 每个二进制数 $最大和存储那些具有比<更多code> $ R 数 1 S IN他们。

Then, we list out every binary number from 0 to $max and store those that have more than $r number of 1s in them.

$allBinary = array();  // the array of binary numbers
for ($i = 0; $i <= $max; $i++)
{
  if (substr_count(decbin($i), "1") >= $r)  // we count the number of ones to determine if they are >= $r
  {
    // we make the length of the binary numbers equal to the number of elements in the array,
    // so that it is easy to select elements from the array, based on which of the digits are 1.
    // we do this by padding zeros to the left.
    $temp = str_pad(decbin($i), $n, "0", STR_PAD_LEFT);
    $allBinary[] = $temp;
  }
}

然后,我们用同样的伎俩上面选择我们的组合元素。我相信,注释说明就够了。

Then, we use the same trick as above to select elements for our combination. I believe the comments explain enough.

$combs = array();  // the array for all the combinations.
$row = array();    // the array of binary digits in one element of the $allBinary array.

foreach ($allBinary as $key => $one)
{
  $combs[$key] = "";
  $row = str_split($one);  // we store the digits of the binary number individually
  foreach ($row as $indx => $digit)
  {
    if ($digit == '1')  // if the digit is 1, then the corresponding element in the array is part of this combination.
    {
      $combs[$key] .= $a[$indx];  // add the array element at the corresponding index to the combination
    }
  }
}

这就是它。你完成了!

And that is it. You are done!

现在,如果你有类似

echo count($combs);

那就给你 29

我只看到你的问题后,在这读了,作为一个新人,我发现这些有用的:

I read up on this only after seeing your question, and as a newcomer, I found these useful:

  • Wikipedia - http://en.wikipedia.org/wiki/Combination
  • Php recursion to get all possibilities of strings
  • Algorithm to return all combinations of k elements from n

此外,这里有一些快速链接到文档,应该帮助人们谁在将来看到这一点:

Also, here are some quick links to the docs, that should help people who see this in the future:

  • http://php.net/manual/en/function.decbin.php
  • http://php.net/manual/en/function.bindec.php
  • http://php.net/manual/en/function.str-pad.php

这篇关于从给定数组PHP - [R元素的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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