Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

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


Distribute your Resume to 500+ Job Boards & Recruiters


7,930 questions

11,045 answers

573 users

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

1 Answer

0 votes
function all_combinations(&$words, &$combinations) {
    for($i = 0; $i < count($words); $i++) {
        $combinations[] = $words[$i];
        $partwords = $words;
        array_splice($partwords, $i, 1);
        $partcomb = array();
        all_combinations($partwords, $partcomb);
        foreach($partcomb as $comb) {
            $combinations[] = $words[$i] . " " . $comb;
        }
    }
}

$combinations = array();
$words = array("php", "java", "c++", "python");
all_combinations($words, $combinations);
echo "<pre>";
var_dump($combinations);
echo "</pre>";

/*
run:

array(64) {
  [0]=>
  string(3) "php"
  [1]=>
  string(8) "php java"
  [2]=>
  string(12) "php java c++"
  [3]=>
  string(19) "php java c++ python"
  [4]=>
  string(15) "php java python"
  [5]=>
  string(19) "php java python c++"
  [6]=>
  string(7) "php c++"
  [7]=>
  string(12) "php c++ java"
  [8]=>
  string(19) "php c++ java python"
  [9]=>
  string(14) "php c++ python"
  [10]=>
  string(19) "php c++ python java"
  [11]=>
  string(10) "php python"
  [12]=>
  string(15) "php python java"
  [13]=>
  string(19) "php python java c++"
  [14]=>
  string(14) "php python c++"
  [15]=>
  string(19) "php python c++ java"
  [16]=>
  string(4) "java"
  [17]=>
  string(8) "java php"
  [18]=>
  string(12) "java php c++"
  [19]=>
  string(19) "java php c++ python"
  [20]=>
  string(15) "java php python"
  [21]=>
  string(19) "java php python c++"
  [22]=>
  string(8) "java c++"
  [23]=>
  string(12) "java c++ php"
  [24]=>
  string(19) "java c++ php python"
  [25]=>
  string(15) "java c++ python"
  [26]=>
  string(19) "java c++ python php"
  [27]=>
  string(11) "java python"
  [28]=>
  string(15) "java python php"
  [29]=>
  string(19) "java python php c++"
  [30]=>
  string(15) "java python c++"
  [31]=>
  string(19) "java python c++ php"
  [32]=>
  string(3) "c++"
  [33]=>
  string(7) "c++ php"
  [34]=>
  string(12) "c++ php java"
  [35]=>
  string(19) "c++ php java python"
  [36]=>
  string(14) "c++ php python"
  [37]=>
  string(19) "c++ php python java"
  [38]=>
  string(8) "c++ java"
  [39]=>
  string(12) "c++ java php"
  [40]=>
  string(19) "c++ java php python"
  [41]=>
  string(15) "c++ java python"
  [42]=>
  string(19) "c++ java python php"
  [43]=>
  string(10) "c++ python"
  [44]=>
  string(14) "c++ python php"
  [45]=>
  string(19) "c++ python php java"
  [46]=>
  string(15) "c++ python java"
  [47]=>
  string(19) "c++ python java php"
  [48]=>
  string(6) "python"
  [49]=>
  string(10) "python php"
  [50]=>
  string(15) "python php java"
  [51]=>
  string(19) "python php java c++"
  [52]=>
  string(14) "python php c++"
  [53]=>
  string(19) "python php c++ java"
  [54]=>
  string(11) "python java"
  [55]=>
  string(15) "python java php"
  [56]=>
  string(19) "python java php c++"
  [57]=>
  string(15) "python java c++"
  [58]=>
  string(19) "python java c++ php"
  [59]=>
  string(10) "python c++"
  [60]=>
  string(14) "python c++ php"
  [61]=>
  string(19) "python c++ php java"
  [62]=>
  string(15) "python c++ java"
  [63]=>
  string(19) "python c++ java php"
}

*/

 




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