2

Hi Guys i have an array like this

$array1 = array('a','b','c','d)

and i want to combine with output like this

  • 'a,b,c'
  • 'a,b,d'
  • 'a,c,d'
  • 'b,c,d'

The problem is to create a function with a variable number and not multiple variable, can anybody help me?

akubabas
  • 433
  • 5
  • 12
  • 27
  • 3
    Ohhkk... Got your problem.. How much candies do you have? – Sougata Bose Jul 09 '15 at 05:22
  • Check this out. I think this will help you [http://stackoverflow.com/questions/3742506/php-array-combinations](http://stackoverflow.com/questions/3742506/php-array-combinations) OR [http://stackoverflow.com/questions/4279722/php-recursion-to-get-all-possibilities-of-strings/8880362#8880362](http://stackoverflow.com/questions/4279722/php-recursion-to-get-all-possibilities-of-strings/8880362#8880362) – Sreelal P Mohan Jul 09 '15 at 05:22
  • just these 4 or goes....many times.Please get all the formats – Unkown_Better Jul 09 '15 at 05:22
  • Do you want to output every combination? Or just the ones you listed? – Will Jul 09 '15 at 05:25

1 Answers1

2

Make a try

[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

[akshay@localhost tmp]$ php permutation_comb.php
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Array
(
    [0] => a,b,c
    [1] => a,b,d
    [2] => a,c,d
    [3] => b,c,d
)

To get all possible combination of chars modify calling part of function like below

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

Which results

[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
)
Akshay Hegde
  • 15,144
  • 2
  • 16
  • 34