PHP获取2个数组之间的所有组合

时间:2014-08-01 12:15:09

标签: php arrays

我有两个简单的PHP数组:

$array1 = array('A', 'B');
$array2 = array('1', '2', '3');

我发现很多算法可以提供以下组合:

A1
A2
A3
B1
B2
B3

我需要的是以下组合:

A1
A2
A3
A12
A13
A23
A123
B1
B2
B3
B12
B13
B23
B123

所以$array1是主数组,需要检查$array2。 请问有人可以帮助我吗?提前完成。

5 个答案:

答案 0 :(得分:1)

<强> TRY:

$array1 = array('A', 'B');
$array2 = array('1', '2', '3');

$num = count($array2);
$comb = array();

//The total number of possible combinations
$total = pow(2, $num);

//Loop through each possible combination  
for ($i = 0; $i < $total; $i++) 
{    
    $flag = '';      
    //For each combination check if each bit is set 
    for ($j = 0; $j < $num; $j++) 
    { 
       //Is bit $j set in $i? 
        if (pow(2, $j) & $i) 
            $flag = $flag.''.$array2[$j];
    }
    if(!empty($flag))
        $comb[] = $flag;
}

// Now $comb has all the possible combinations of $array2
// Just loop it through the other array and concat    

$result = array();    
foreach($array1 as $val)
{
    foreach($comb as $co)
        $result[] = $val."".$co;
}

print_r($result);


结果:

Array
(
    [0] => A1
    [1] => A2
    [2] => A12
    [3] => A3
    [4] => A13
    [5] => A23
    [6] => A123
    [7] => B1
    [8] => B2
    [9] => B12
    [10] => B3
    [11] => B13
    [12] => B23
    [13] => B123
)


DEMO:

http://3v4l.org/LdNlI

答案 1 :(得分:0)

foreach ($array1 as $ch){

 foreach ($array2 as &num){ 

echo "".$ch.$num."<br>"; }

 for ($i=0;$i<count($array2);$i++){

 for ($i2=$i+1;$i2<count($array2);i2++){

echo "".$ch.$array2[$i].$array2[$i2]."<br>"; }

 }

 echo $ch;

 foreach ($array2 as &num){ 

echo $num; }

 }

答案 2 :(得分:0)

$array1 = array('A', 'B');
$array2 = array('1', '2', '3');

foreach($array1 as $arr1) {
  $nbIte = 0;
  echo $arr1."\n";
  $concat = $arr1;
  $i = 0;
  while( $i < sizeof($array2)){
    if($nbIte == 0)
       echo $concat.$array2[$i]."\n";
    $save = $i;
    $i++;
       while($i < sizeof($array2)){
          echo $concat.$array2[$save].$array2[$i]."\n";
          $i++;
       }
   $i = $save +1 ;
   if($i == sizeof($array2)){
      if($nbIte < sizeof($array2)){
         $concat .= $array2[$nbIte];
         $nbIte ++;
         $i = $nbIte;
       }
   }

  }
}

答案 3 :(得分:0)

简单递归,)

<?php

$main = array("A","B");
$extender = array("1","2","3","4");

function combinations($elements)
{
    $return_combinations = array();
    $copy = $elements;

    foreach ($elements as $k => $v) {
        $return_combinations[] = [$v];
        unset($copy[$k]);
        foreach (combinations($copy) as $comb) {        
            $return_combinations[] = array_merge([$v], $comb);
        }
    }

    return $return_combinations;
}

foreach($main as $v) {
    foreach(combinations($extender) as $comb) {
        echo $v;
        echo implode('',$comb);
        echo "\r\n";
    }
}

-

A1
A12
A123
A1234
A124
A13
A134
A14
A2
A23
A234
A24
A3
A34
A4
B1
B12
B123
B1234
B124
B13
B134
B14
B2
B23
B234
B24
B3
B34
B4

-

或三个元素(如op请求)

A1
A12
A123
A13
A2
A23
A3
B1
B12
B123
B13
B2
B23
B3

答案 4 :(得分:0)

@Parag Tyagi:非常感谢!!

我修改了一些代码并使其成为一个函数:

function get_arrays_combinations($array1, $array2) {
  $num = count($array2);
  $comb = array();

  // The total number of possible combinations.
  $total = pow(2, $num);

  // Loop through each possible combination.
  for ($i = 0; $i < $total; $i++) {
    $flag = '';
    for ($j = 0; $j < $num; $j++) { // For each combination check if each bit is set.
      if (pow(2, $j) & $i) { // Is bit $j set in $i? 
        if (empty($flag)) {
          $flag = $array2[$j];
        }
        else {
          $flag = $flag . "-" . $array2[$j];
        }
      }
    }
    if(!empty($flag)) {
      $comb[] = $flag;
    }
  }

  // Now $comb has all the possible combinations of $array2.
  // Just loop it through the other array and concat.
  $result = array();    
  foreach($array1 as $val) {
    foreach($comb as $co) {
      $result[] = $val . "-" . $co;
    }
  }
  return $result;
}
$array1 = array('A', 'B');
$array2 = array('1', '2', '3');
$combos = get_arrays_combinations($array1, $array2);

$ combos输出:

Array
(
    [0] => A-1
    [1] => A-2
    [2] => A-1-2
    [3] => A-3
    [4] => A-1-3
    [5] => A-2-3
    [6] => A-1-2-3
    [7] => B-1
    [8] => B-2
    [9] => B-1-2
    [10] => B-3
    [11] => B-1-3
    [12] => B-2-3
    [13] => B-1-2-3
)

非常感谢,也感谢其他用户。 这是我在StackOverflow中的第一个问题(不是访问),这是一次非常棒的经历:)