voteforpedro
voteforpedro

Reputation: 187

How to get ALL combinations of a list of words using ANY number of words

I have searched but I can't find anything that matches my query. I have seen lots of solutions where people want all combinations of numbers/words that use ALL the options, but none like this...

Here's an example:

apple pear

This should generate:

apple
pear
apple pear
pear apple

Or even...

apple pear banana

apple
pear
banana
apple pear
apple banana
pear banana
...
...
banana pear apple

The key is, ALL possible combinations that use any of the words zero or one times in ANY order. :)

Upvotes: 1

Views: 5406

Answers (2)

Naftali
Naftali

Reputation: 146310

FINAL ANSWER AT THE BOTTOM


Pseudocode (has not been tested)

$str = "apple pear banana";
$str_splode = explode(' ',$str);

echo showCombo($str_splode[0], $str_splode);

function showCombo($str, $arr){
    $ret = '';
    foreach($arr as $val){
       if($val != $str)
           $ret .= $str.showCombo($val, $arr);
    }
    return $ret;
}

Running code: http://codepad.org/IUPJbhI7

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
print_r(showCombo(array(), $str_splode));

function showCombo($str_arr, $arr){
    $ret = array();
    foreach($arr as $val){
       if(!in_array($val, $str_arr)){
           $temp = $str_arr;
           $temp[] = $val;
           print_r($temp);
           $comb = showCombo($temp, $arr);
           if(count($comb) > 0)
              $ret[] = $comb;
       }
    }
    return $ret;
}
?>

This returns all possible combinations


Or this one looks better: http://codepad.org/KCLeRUYs

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
print_r(showCombo(array(), $str_splode));

function showCombo($str_arr, $arr){
    $ret = array();
    foreach($arr as $val){
       if(!in_array($val, $str_arr)){
           $temp = $str_arr;
           $temp[] = $val;
           $ret[$val] = $temp;
           $ret[$val][] = showCombo($temp, $arr);
       }
    }
    return $ret;
}

?>

Or if you want to look at flat keys: http://codepad.org/95aNQzXB


Final Answer:

And this one lists them all: http://codepad.org/vndOI9Yj

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
$combos = showCombo(array(), $str_splode);
foreach($combos as $key=>$array){
    echo $key.PHP_EOL;
    displayArrayByKey($key, $array);
}


function displayArrayByKey($str, $arr){
    foreach($arr as $key=>$array){
          $string = $str. " " . $key;
          echo $string . PHP_EOL; 
          if(count($array)> 0){
              displayArrayByKey($string, $array);
          }
    }
}

function showCombo($str_arr, $arr){
    $ret = array();
    foreach($arr as $val){
       if(!in_array($val, $str_arr)){
           $temp = $str_arr;
           $temp[] = $val;
           $ret[$val] = showCombo($temp, $arr);
       }
    }
    return $ret;
}

?>

Upvotes: 3

leticia
leticia

Reputation: 2388

You can download this class: http://pear.php.net/package/Math_Combinatorics

and use it like:

$combinatorics = new Math_Combinatorics;

$words_arr = array(
    'one'   => 'a',
    'two'   => 'b',
    'three' => 'c',
    'four'  => 'd',
    );

for ($i=count($words_arr)-1;$i>=1;$i--) {
    echo '<br><br>' . $i . ':<br>';
    $combinations_arr = $combinatorics->combinations($words_arr, $i);
    foreach ($combinations_arr as $combinations_arr_item) {
        echo implode(', ', $combinations_arr_item) . '<br>';
    }
}

Upvotes: 1

Related Questions