All possible combination of given letters

Ok, so this is what i want to do and i really need your help guys, because i’m new with php…

So lets say i have 5 letters: A; B; C; D and E. and what i want a php script to do is to generate all possible combinations of those letters with no repeats. For example:

ABCDE
ECDBA
CBAED

and so on, until theres no combinations left (543*2=120 combinations)
so if you can, please help…

what you want to do is count in a different base (ie: decimal=base 10, hex=base 16, binary=base 2). so, build a ticker similar to an analog speedometer.

take your variables (A, B, C, D, E) and put them into an array. count the array elements and construct a string of that length, starting with 0-0-0-0-0. next build a function or class that increments the 1st digit. when it reaches 4, it increments the next digit by one and returns itself to 0. the function/class will probably need to be recursive. have it terminate at 4-4-4-4-4

by the way: props for the creative problem. it took me quite a while to figure out the best way to think about it

well, there’s a problem… you see i want that all letters would appear only once per combination. so letters combination:
A B C D E is cool, but
A A B C E is not cool :slight_smile:
so making speedometer would’nt work, because there would be such comninations like:
0-0-0-0-0
0-0-0-0-1 and so on…

Here’s a class i’ve used before to do this, i can’t remember who the original author was but the credit goes to them anyway :wink:


<?php

class stringPerms
{
  var $results = array();
  var $theList = array();
  var $aString = '';

  function stringPerms($aString){
    $this->theList = $this->make_list($aString);
  }

  function make_list($aString){
    $x = array();
    for($counter = 0; $counter < strlen($aString); $counter++){
      $x[$counter] = substr($aString, $counter, 1);
    }
    return $x;
  }

  function get_combinations(){
    return $this->get_all($this->theList);
  }

  function get_all($ar, $app = ''){
    if (count($ar) > 0){
      $x = 0;
      while ($x < count($ar)){
        $tmp = array_slice($ar,0,count($ar));
        array_splice($tmp, $x, 1);
        $sub = count($this->theList) - count($ar);
        $app = substr($app,0,$sub) . $ar[$x];
        $this->get_all(array_slice($tmp,0,count($tmp)), $app);
        $x = $x + 1;
      }
      if (count($ar) == 1){
        array_push($this->results, $app);
        $app = '';
      }
    }
    if (count($this->results) == $this->getResults()){
      return $this->results;
    }
  }

  function getResults(){
    return $this->factorial(count($this->theList));
  }

  function factorial($x){
    $f = 1;
    while($x > 0){
      $f = $f*$x;
      $x = $x - 1;
    }
    return $f;
  }

}


//example useage
$myString = new stringPerms('ABCDE');
$perms = $myString->get_combinations();
foreach($perms as $key => $value){
  echo $key+1 .": $value<br>";
}

?>

Yeah, that’s IT! :smiley: Thanks!

On a side note, if you have a scientific calculator handy it should have an ! (exclamation mark) button which is the factorial function. It returns the number of combinations of how much discrete information you have. Like ABCDE would have 5, so that’s 5 factorial (5!) which gives 120. You probably already knew that, but it might be interesting to someone else :smiley:

Well, that’s the only thing i know how to do :smiley: so no problemo! :slight_smile: