user3899317
user3899317

Reputation: 23

PHP Get all combinations between 2 arrays

I have 2 simple PHP arrays:

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

I have found a lot of algorithms that give you the following combinations:

A1
A2
A3
B1
B2
B3

But what I need is the following combinations:

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

So $array1 is the main array here and needs to check $array2. Can somebody help me, please? Thanx in advance.

Upvotes: 1

Views: 824

Answers (5)

user3899317
user3899317

Reputation: 23

@Parag Tyagi: Thanks a lot!!

I modified a little your code and made it a function:

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 outputs:

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
)

Thank you very much, also to other users. This is my first question (not visit) in StackOverflow and has been a wonderful experience :)

Upvotes: 0

Xevelion
Xevelion

Reputation: 869

Simply recursive ,)

<?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

-

or for three elements (like op requested)

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

Upvotes: 0

Parag Tyagi
Parag Tyagi

Reputation: 8960

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);


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

Upvotes: 1

Ko2r
Ko2r

Reputation: 1631

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

  }
}

Upvotes: 0

mark
mark

Reputation: 89

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

 }

Upvotes: 0

Related Questions