Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

8,367 questions

11,628 answers

573 users

How to generate all possible combinations from array of letters in PHP

1 Answer

0 votes
function all_combinations(&$letters, &$combinations) {
    for($i = 0; $i < count($letters); $i++) {
        $combinations[] = $letters[$i];
        $partlet = $letters;
        array_splice($partlet, $i, 1);
        $partcomb = array();
        all_combinations($partlet, $partcomb);
        foreach($partcomb as $comb) {
            $combinations[] = $letters[$i] . $comb;
        }
    }
}
 
 
$combinations = array();
$letters = array("a", "b", "c", "d");
all_combinations($letters, $combinations);
echo "<pre>";
var_dump($combinations);
echo "</pre>";

/*
run:

array(64) {
  [0]=>
  string(1) "a"
  [1]=>
  string(2) "ab"
  [2]=>
  string(3) "abc"
  [3]=>
  string(4) "abcd"
  [4]=>
  string(3) "abd"
  [5]=>
  string(4) "abdc"
  [6]=>
  string(2) "ac"
  [7]=>
  string(3) "acb"
  [8]=>
  string(4) "acbd"
  [9]=>
  string(3) "acd"
  [10]=>
  string(4) "acdb"
  [11]=>
  string(2) "ad"
  [12]=>
  string(3) "adb"
  [13]=>
  string(4) "adbc"
  [14]=>
  string(3) "adc"
  [15]=>
  string(4) "adcb"
  [16]=>
  string(1) "b"
  [17]=>
  string(2) "ba"
  [18]=>
  string(3) "bac"
  [19]=>
  string(4) "bacd"
  [20]=>
  string(3) "bad"
  [21]=>
  string(4) "badc"
  [22]=>
  string(2) "bc"
  [23]=>
  string(3) "bca"
  [24]=>
  string(4) "bcad"
  [25]=>
  string(3) "bcd"
  [26]=>
  string(4) "bcda"
  [27]=>
  string(2) "bd"
  [28]=>
  string(3) "bda"
  [29]=>
  string(4) "bdac"
  [30]=>
  string(3) "bdc"
  [31]=>
  string(4) "bdca"
  [32]=>
  string(1) "c"
  [33]=>
  string(2) "ca"
  [34]=>
  string(3) "cab"
  [35]=>
  string(4) "cabd"
  [36]=>
  string(3) "cad"
  [37]=>
  string(4) "cadb"
  [38]=>
  string(2) "cb"
  [39]=>
  string(3) "cba"
  [40]=>
  string(4) "cbad"
  [41]=>
  string(3) "cbd"
  [42]=>
  string(4) "cbda"
  [43]=>
  string(2) "cd"
  [44]=>
  string(3) "cda"
  [45]=>
  string(4) "cdab"
  [46]=>
  string(3) "cdb"
  [47]=>
  string(4) "cdba"
  [48]=>
  string(1) "d"
  [49]=>
  string(2) "da"
  [50]=>
  string(3) "dab"
  [51]=>
  string(4) "dabc"
  [52]=>
  string(3) "dac"
  [53]=>
  string(4) "dacb"
  [54]=>
  string(2) "db"
  [55]=>
  string(3) "dba"
  [56]=>
  string(4) "dbac"
  [57]=>
  string(3) "dbc"
  [58]=>
  string(4) "dbca"
  [59]=>
  string(2) "dc"
  [60]=>
  string(3) "dca"
  [61]=>
  string(4) "dcab"
  [62]=>
  string(3) "dcb"
  [63]=>
  string(4) "dcba"
}

*/

 




answered Jul 3 by avibootz
edited Jul 4 by avibootz
...