0

Possible Duplicates:
PHP: How to get all possible combinations of 1D array?
Generate all possible combinations using a set of strings

$s = 'A,B,C';

Given a set of strings, how do you compute that it's possible to have AAA BBB CCC ABC ACB BCA etc.?

Community
  • 1
  • 1
IMB
  • 15,163
  • 19
  • 82
  • 140
  • @andrewsi I'm quite clueless on what PHP functions to use. I've tried `foreach` and pick a random entry x times but how do you remove duplicate and stop when there's no more to pick. – IMB Sep 28 '12 at 18:53
  • 1
    Don't go random, go in order. – moonwave99 Sep 28 '12 at 18:53
  • Convert the string into an array using [`explode()`](http://php.net/manual/en/function.explode.php) and refer to the dupes. – NullUserException Sep 28 '12 at 18:56

3 Answers3

0

In c++ would be:

do{
cout << s << endl;
}while(next_permutation(s,s+s.size());

so just go through all permutations.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
0

Find or write a function that contains a recursive loop over the array. That should return you an array with all possible combos.

dudewad
  • 476
  • 4
  • 8
0
$s = explode($s,",")


function recursion($string, $depth, $maxdepth, $s)
{
   if($depth == $maxdepth)
       echo $string
   else
   {
      for($i = 0;$i < sizeof($s); $i++)
      {
         recursion($string+$s[$i], $depth+1, $maxdepth, $s)
      }
   }
}

recursion("",0, sizeof($s), $s)

of course you could make $maxdepth and $s global variables to exclude them from the parameter list

Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55