11

Here is my code to get all possibilities:

$seq[1] = 'd';
$seq[2] = 'f';
$seq[3] = 'w';
$seq[4] = 's';

for($i = 1; $i < 5; $i++)
{
    $s['length_1'][] = $seq[$i];
    $c1++;

    for($i2 = $i+1; $i2 < 5; $i2++)
    {
        $s['length_2'][] = $seq[$i].$seq[$i2]; 
        $last = $seq[$i].$seq[$i2]; 
        $c2++;

        for($i3 = $i2+1; $i3 < 5; $i3++)
        { 
            $s['length_3'][] = $last.$seq[$i3];
            $last = $last.$seq[$i3];    
            $c3++;

            for($i4 = $i3+1; $i4 < 5; $i4++)
            {
                $s['length_4'][] = $last.$seq[$i4];   
                $c4++;  
            }
        }
    }
}

for($i = 0; $i < $c1; $i++)
    echo $s['length_1'][$i].'<br>'; 

for($i = 0; $i < $c2; $i++)
    echo $s['length_2'][$i].'<br>';   

for($i = 0; $i < $c3; $i++)
    echo $s['length_3'][$i].'<br>';  

for($i = 0; $i < $c4; $i++)
    echo $s['length_4'][$i].'<br>';    

But if I want to add more, then I will have to add one more loop. So, how can I do it with recursion? I try, I try, but I really can't do it. Please help and post example as simple as possible.

Thank you.

jmj
  • 225,392
  • 41
  • 383
  • 426
good_evening
  • 19,773
  • 60
  • 178
  • 288

5 Answers5

22

One algorithm is here,

function getCombinations($base,$n){

$baselen = count($base);
if($baselen == 0){
    return;
}
    if($n == 1){
        $return = array();
        foreach($base as $b){
            $return[] = array($b);
        }
        return $return;
    }else{
        //get one level lower combinations
        $oneLevelLower = getCombinations($base,$n-1);

        //for every one level lower combinations add one element to them that the last element of a combination is preceeded by the element which follows it in base array if there is none, does not add
        $newCombs = array();

        foreach($oneLevelLower as $oll){

            $lastEl = $oll[$n-2];
            $found = false;
            foreach($base as  $key => $b){
                if($b == $lastEl){
                    $found = true;
                    continue;
                    //last element found

                }
                if($found == true){
                        //add to combinations with last element
                        if($key < $baselen){

                            $tmp = $oll;
                            $newCombination = array_slice($tmp,0);
                            $newCombination[]=$b;
                            $newCombs[] = array_slice($newCombination,0);
                        }

                }
            }

        }

    }

    return $newCombs;


}

I know it is not efficent in any way, but using in small sets should not be a problem

first base parameter is an array containing elements to be considered when generating combinations.

for simple usage and output:

var_dump(getCombinations(array("a","b","c","d"),2));

and output is

array
  0 => 
    array
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
  1 => 
    array
      0 => string 'a' (length=1)
      1 => string 'c' (length=1)
  2 => 
    array
      0 => string 'a' (length=1)
      1 => string 'd' (length=1)
  3 => 
    array
      0 => string 'b' (length=1)
      1 => string 'c' (length=1)
  4 => 
    array
      0 => string 'b' (length=1)
      1 => string 'd' (length=1)
  5 => 
    array
      0 => string 'c' (length=1)
      1 => string 'd' (length=1)

To list all subsets of an array, using this combinations algorithm just execute

$base =array("a","b","c","d");

for($i = 1; $i<=4 ;$i++){
    $comb = getCombinations($base,$i);

    foreach($comb as $c){
        echo implode(",",$c)."<br />";
    }

}

And output is

a
b
c
d
a,b
a,c
a,d
b,c
b,d
c,d
a,b,c
a,b,d
a,c,d
b,c,d
a,b,c,d
Kemal Dağ
  • 2,687
  • 19
  • 26
  • In a list like Array ( [0] => 10 [1] => 10 [2] => 14 [3] => 39 [4] => 39 [5] => 39 [6] => 39 [7] => 40 [8] => 40 [9] => 42 [10] => 42 [11] => 45 ) How can I create permutations like 10, 10, 14 and not just 10, 14, 39? – Katie Apr 30 '13 at 20:41
15

Here's a simple algo. Iterate from 1 to 2count(array)-1. On each iteration, if j-th bit in a binary representation of the loop counter is equal to 1, include j-th element in a combination.

As PHP needs to be able to calculate 2count(array) as an integer, this may never exceed PHP_INT_MAX. On a 64-bit PHP installation your array cannot have more than 62 elements, as 262 stays below PHP_INT_MAX while 263 exceeds it.

EDIT: This computes all possible combinations, not permutations (ie, 'abc' = 'cba'). It does so by representing the original array in binary and "counting up" from 0 to the binary representation of the full array, effectively building a list of every possible unique combination.

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

$len  = count($a);
$list = array();

for($i = 1; $i < (1 << $len); $i++) {
    $c = '';
    for($j = 0; $j < $len; $j++)
        if($i & (1 << $j))
            $c .= $a[$j];
    $list[] = $c;
}

print_r($list);
Martijn
  • 3
  • 1
  • 5
user187291
  • 50,823
  • 18
  • 89
  • 127
  • 1
    this won't test all possibilities.. for example if only going for [a, b] it will output this: a b ab what about "ba"? –  Nov 26 '10 at 09:50
  • 3
    "ba" is same as "ab" in term of combinations or sets. What you looking for is permutations. – Kemal Dağ Apr 09 '13 at 14:02
  • @evening http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Katie Apr 30 '13 at 21:04
  • i got a memory error when i use your code with `$a = range('A', 'Z');`, the code of @KemalDağ below works fine. – Yoann Kergall Jan 24 '18 at 18:23
4

Here it is:

<?php
function combinations($text,$space)
{
    // $text is a variable which will contain all the characters/words of which  we want to make all the possible combinations
    // Let's make an array which will contain all the characters
    $characters=explode(",", $text);
    $x=count($characters);

    $comb = fact($x);

    // In this loop we will be creating all the possible combinations of the  positions that are there in the array $characters

    for ($y=1; $y<= $comb; $y++)
    {
        $ken = $y-1;
        $f = 1;
        $a = array();
        for($iaz=1; $iaz<=$x; $iaz++)
            {
                $a[$iaz] = $iaz;
                $f = $f*$iaz;
            }
        for($iaz=1; $iaz<=$x-1; $iaz++)
            {
                $f = $f/($x+1-$iaz);
                $selnum = $iaz+$ken/$f;
                $temp = $a[$selnum];
                for($jin=$selnum; $jin>=$iaz+1; $jin--)
                    {
                        $a[$jin] = $a[$jin-1];
                    }
                $a[$iaz] = $temp;
                $ken = $ken%$f;
            }
        $t=1;

           // Let’s start creating a word combination: we have all the  necessary positions
        $newtext="";

        // Here is the while loop that creates the word combination
        while ($t<=$x)
            {
                $newtext.=$characters[$a[$t]-1]."$space";
                $t++;
            }
        $combinations[] =  $newtext ;
    }

        return $combinations;

}

function fact($a){
if ($a==0) return 1;
else return $fact = $a * fact($a-1);
}

$a = combinations("d,f,w,s","");
    foreach ($a as $v) {
            echo "$v"."\n";
    }

?>

Output:

dfws
dfsw
dwfs
dwsf
dsfw
dswf
fdws
fdsw
fwds
fwsd
fsdw
fswd
wdfs
wdsf
wfds
wfsd
wsdf
wsfd
sdfw
sdwf
sfdw
sfwd
swdf
swfd

Also, read this;

Community
  • 1
  • 1
shamittomar
  • 43,068
  • 12
  • 72
  • 76
  • uses a lot of memory :/ "PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in..." – Katie Apr 30 '13 at 21:07
1

You can do this:

function combinations($arr) {
    $combinations = array_fill(0, count($arr)+1, array());
    $combinations[0] = array('');
    for ($i = 0, $n = count($arr); $i < $n; ++$i) {
        for ($l = $n-$i; $l > 0; --$l) {
            $combinations[$l][] = implode('', array_slice($arr, $i, $l));
        }
    }
    return $combinations;
}

Here’s an example:

$arr = array('d', 'f', 'w', 's');
var_dump(combinations($arr));

This produces the following array:

array(
    array(''),                 // length=0
    array('d', 'f', 'w', 's'), // length=1
    array('df', 'fw', 'ws'),   // length=2
    array('dfw', 'fws'),       // length=3
    array('dfws')              // length=4
)

A brief explanation:

For each i with 0 ≤ i < n, get all sub-arrays arr‍[i,‍i+‍l] with each possible length of 0 < ln - i.

Gumbo
  • 594,236
  • 102
  • 740
  • 814
0

Here is my function to print all possible character combinations:

function printCombinations($var, $begin = 0, $preText = "") {
    for($i = $begin; $i < count($var); $i++) {
        echo $preText . $var[$i] . "\n";
        if(($i+1) < count($var))
            printCombinations($var, $i+1, $preText . $var[$i]);
    }
}

printCombinations(array('a','b','c','d','e'));
winsetter
  • 181
  • 1
  • 4