Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 生成组合数组_Php_Arrays_Iteration_Combinations - Fatal编程技术网

Php 生成组合数组

Php 生成组合数组,php,arrays,iteration,combinations,Php,Arrays,Iteration,Combinations,嗨,伙计们,我有一个这样的阵列 $array1 = array('a','b','c','d) 我想结合这样的输出 $array1 = array('a','b','c','d) “a,b,c” “a,b,d” “a,c,d” “b,c,d” 问题是要创建一个带有变量号而不是多个变量的函数,有人能帮我吗?试试看 [akshay@localhost tmp]$ cat permutation_comb.php <?php function _perm($comb,$arr) {

嗨,伙计们,我有一个这样的阵列

$array1 = array('a','b','c','d)
我想结合这样的输出

$array1 = array('a','b','c','d)
  • “a,b,c”
  • “a,b,d”
  • “a,c,d”
  • “b,c,d”
问题是要创建一个带有变量号而不是多个变量的函数,有人能帮我吗?

试试看

[akshay@localhost tmp]$ cat permutation_comb.php
<?php

function _perm($comb,$arr)
{

    $arr_len = count($arr);
    $comb = intval($comb);
    if ($comb > $arr_len)
    {
       $p = 0;
    }
 elseif ($arr_len == $comb)
    {
       $p = 1;
    }
   else {
        if ($comb >= $arr_len - $comb)
        {
            $l = $comb+1;
            for ($i = $l+1 ; $i <= $arr_len ; $i++)
                $l *= $i;
                $m  = 1;
            for ($i = 2 ; $i <= $arr_len-$comb ; $i++)
                $m *= $i;
        }
   else {
            $l = ($arr_len-$comb) + 1;
            for ($i = $l+1 ; $i <= $arr_len ; $i++)
                $l *= $i;
                $m  = 1;
            for ($i = 2 ; $i <= $comb ; $i++)
                $m *= $i;           
        }
       }

     if(!isset($p)){ $p = $l/$m ; }

     $out = array_fill(0, $p, array_fill(0, $comb, '') );

     $t = array();
     for ($i = 0 ; $i < $comb ; $i++)
            $t[$i] = $i;

     $out[0] = $t;
     for ($i = 1 ; $i < $p ; $i++)
     {
        if ($t[$comb-1] != count($arr)-1)
        {
            $t[$comb-1]++;
        }
   else {
            $xx = -1;
            for ($j = $comb-2 ; $j >= 0 ; $j--)
            if ($t[$j]+1 != $t[$j+1])
                {
                    $xx = $j;
                    break;
                }

            if ($xx == -1)
                break;

            $t[$xx]++;
            for ($j = $xx+1 ; $j < $comb ; $j++)   
                $t[$j] = $t[$xx]+$j-$xx;
        }
        $out[$i] = $t;
    }
    for ($i = 0 ; $i < $p ; $i++)
        for ($j = 0 ; $j < $comb ; $j++)
            $out[$i][$j] = $arr[$out[$i][$j]];  
    return $out;
}

    $Input  =   array('a','b','c','d');
    $output =   array_map(function($a){ return implode(",",$a); },_perm(3, $Input));


   // Input
   print_r($Input);

   // Combination output
   print_r($output);
?> 
要获得所有可能的字符组合,请修改函数的调用部分,如下所示

$output =   array();    
for($i=1; $i<=count($Input); $i++)
{
  $output =  array_merge($output,  array_map(function($a){ return implode(",",$a); },_perm($i, $Input)) ) ;
} 

香港。。。有你的问题。。你有多少糖果?看看这个。我想这会帮助你,或者仅仅是这4个或goes…很多次。请获取所有的格式。你想输出每个组合吗?或者仅仅是你列出的那些?好的,thax,它在我的代码中起作用:)祝你有愉快的一天
[akshay@localhost tmp]$ php permutation_comb.php
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => a,b
    [5] => a,c
    [6] => a,d
    [7] => b,c
    [8] => b,d
    [9] => c,d
    [10] => a,b,c
    [11] => a,b,d
    [12] => a,c,d
    [13] => b,c,d
    [14] => a,b,c,d
)