All combinations of r elements from a given php array

Given an array such as the following

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

I am looking for a way to generate all possible combinations with the minimum number of elements needed in each combination r. (for example, if r = 5, then it will return all possible combinations containing at least 5 elements)

0
source share
3 answers

A combination can be expressed as

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

First we define $nas the number of elements in the array. A $ris 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);

$max , $n. , $n = 3, $max = (111) 2= 7. $maxBinary $n 1. $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 $max , $r 1.

$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;
  }
}

, , . , .

$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
    }
  }
}

. !

, -

echo count($combs);

29.

:

, , :

, , , :

+1

k n , :

function combinationsOf($k, $xs){
        if ($k === 0)
            return array(array());
        if (count($xs) === 0)
            return array();
        $x = $xs[0];
        $xs1 = array_slice($xs,1,count($xs)-1);
        $res1 = combinationsOf($k-1,$xs1);
        for ($i = 0; $i < count($res1); $i++) {
            array_splice($res1[$i], 0, 0, $x);
        }
        $res2 = combinationsOf($k,$xs1);
        return array_merge($res1, $res2);
    }

, k out n x, C(k-1, xs\{x}) , x (.. res1) C(k,xs\{xs}), x (.. res2 ).

:

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

function combinationsOf($k, $xs){
        if ($k === 0)
            return array(array());
        if (count($xs) === 0)
            return array();
        $x = $xs[0];
        $xs1 = array_slice($xs,1,count($xs)-1);
        $res1 = combinationsOf($k-1,$xs1);
        for ($i = 0; $i < count($res1); $i++) {
            array_splice($res1[$i], 0, 0, $x);
        }
        $res2 = combinationsOf($k,$xs1);
        return array_merge($res1, $res2);
    }

print_r ($array);
print_r(combinationsOf(5,$array));
//print_r(combinationsOf(5,$array)+combinationsOf(6,$array)+combinationsOf(7,$array));
+2
    function arrToBit(Array $element) {
        $bit = '';
        foreach ($element as $e) {
            $bit .= '1';
        }
        $length = count($element);
        $num = bindec($bit);
        $back = [];
        while ($num) {
            $back[] = str_pad(decbin($num), $length, '0', STR_PAD_LEFT);
            $num--;
        }
        //$back[] = str_pad(decbin(0), $length, '0', STR_PAD_LEFT);
        return $back;
    }

    function bitToArr(Array $element, $bit) {
        $num = count($element);
        $back = [];
        for ($i = 0; $i < $num; $i++) {
            if (substr($bit, $i, 1) == '1') {
                $back[] = $element[$i];
            }
        }
        return $back;
    }

    $tags = ['a', 'b', 'c'];
    $bits = arrToBit($tags);
    $combination = [];
    foreach ($bits as $b) {
        $combination[] = bitToArr($tags, $b);
    }
    var_dump($combination);
0
source

Source: https://habr.com/ru/post/1568145/


All Articles