35

Possible Duplicate:
algorithm that will take numbers or words and find all possible combinations
Combinations, Dispositions and Permutations in PHP

I've read/tried alot of the suggested answers on SO, which none of them solves the problem

$array = array('Alpha', 'Beta', 'Gamma');

How to get all possible combinations?

Expected output:

array('Alpha',
      'Beta',
      'Gamma',
      'Alpha Beta',
      'Alpha Gamma',
      'Beta Alpha',
      'Beta Gamma',
      'Gamma Alpha',
      'Gamma Beta',
      'Alpha Beta Gamma',
      'Alpha Gamma Beta',
      'Beta Alpha Gamma',
      'Beta Gamma Alpha',
      'Gamma Alpha Beta',
      'Gamma Beta Alpha')

Note: The answer I'm looking for should include all combinations and all different arrangements. For example: 'Alpha Beta' and 'Beta Alpha' are 2 different strings and both should be in the output array.

Thanks in advance

7
  • @Juhana OP also want's single words Commented May 31, 2012 at 13:23
  • Can you explode the array, use a foreach along with the total count and cycle through them? Commented May 31, 2012 at 13:26
  • @Juhana plz don't close the question, other questions could be similar, but not the same Commented May 31, 2012 at 13:30
  • What exactly didn't work in your attempts? You could start from there. Commented May 31, 2012 at 13:31
  • @evilReiko The duplicate doesn't have repeated elements. Commented May 31, 2012 at 13:31

1 Answer 1

55

I believe your professor will be happier with this solution:

<?php

$array = array('Alpha', 'Beta', 'Gamma', 'Sigma');

function depth_picker($arr, $temp_string, &$collect) {
    if ($temp_string != "") 
        $collect []= $temp_string;

    for ($i=0, $iMax = sizeof($arr); $i < $iMax; $i++) {
        $arrcopy = $arr;
        $elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
        if (sizeof($arrcopy) > 0) {
            depth_picker($arrcopy, $temp_string ." " . $elem[0], $collect);
        } else {
            $collect []= $temp_string. " " . $elem[0];
        }   
    }   
}

$collect = array();
depth_picker($array, "", $collect);
print_r($collect);

?>

This solves it:

Array
(
    [0] =>  Alpha
    [1] =>  Alpha Beta
    [2] =>  Alpha Beta Gamma
    [3] =>  Alpha Beta Gamma Sigma
    [4] =>  Alpha Beta Sigma
    [5] =>  Alpha Beta Sigma Gamma
    [6] =>  Alpha Gamma
    [7] =>  Alpha Gamma Beta
    [8] =>  Alpha Gamma Beta Sigma
    [9] =>  Alpha Gamma Sigma
    [10] =>  Alpha Gamma Sigma Beta
    [11] =>  Alpha Sigma
    [12] =>  Alpha Sigma Beta
    [13] =>  Alpha Sigma Beta Gamma
    [14] =>  Alpha Sigma Gamma
    [15] =>  Alpha Sigma Gamma Beta
    [16] =>  Beta
    [17] =>  Beta Alpha
    [18] =>  Beta Alpha Gamma
    [19] =>  Beta Alpha Gamma Sigma
    [20] =>  Beta Alpha Sigma
    [21] =>  Beta Alpha Sigma Gamma
    [22] =>  Beta Gamma
    [23] =>  Beta Gamma Alpha
    [24] =>  Beta Gamma Alpha Sigma
    [25] =>  Beta Gamma Sigma
    [26] =>  Beta Gamma Sigma Alpha
    [27] =>  Beta Sigma
    [28] =>  Beta Sigma Alpha
    [29] =>  Beta Sigma Alpha Gamma
    [30] =>  Beta Sigma Gamma
    [31] =>  Beta Sigma Gamma Alpha
    [32] =>  Gamma
    [33] =>  Gamma Alpha
    [34] =>  Gamma Alpha Beta
    [35] =>  Gamma Alpha Beta Sigma
    [36] =>  Gamma Alpha Sigma
    [37] =>  Gamma Alpha Sigma Beta
    [38] =>  Gamma Beta
    [39] =>  Gamma Beta Alpha
    [40] =>  Gamma Beta Alpha Sigma
    [41] =>  Gamma Beta Sigma
    [42] =>  Gamma Beta Sigma Alpha
    [43] =>  Gamma Sigma
    [44] =>  Gamma Sigma Alpha
    [45] =>  Gamma Sigma Alpha Beta
    [46] =>  Gamma Sigma Beta
    [47] =>  Gamma Sigma Beta Alpha
    [48] =>  Sigma
    [49] =>  Sigma Alpha
    [50] =>  Sigma Alpha Beta
    [51] =>  Sigma Alpha Beta Gamma
    [52] =>  Sigma Alpha Gamma
    [53] =>  Sigma Alpha Gamma Beta
    [54] =>  Sigma Beta
    [55] =>  Sigma Beta Alpha
    [56] =>  Sigma Beta Alpha Gamma
    [57] =>  Sigma Beta Gamma
    [58] =>  Sigma Beta Gamma Alpha
    [59] =>  Sigma Gamma
    [60] =>  Sigma Gamma Alpha
    [61] =>  Sigma Gamma Alpha Beta
    [62] =>  Sigma Gamma Beta
    [63] =>  Sigma Gamma Beta Alpha
)
Sign up to request clarification or add additional context in comments.

6 Comments

I know that my question sounds like a homework, but it's not! LOL! I know the solution hides within recursion but I'm not good at it at all. Perfect answer, worths a medal!
Thanks broseph means a lot to me. May I ask what it is used for if not for homework :) ?
This code is the key to improve my site search engine, so that it can suggest "familiar" keyword for the user
You could improve your function slightly by making temp_string an optional parameter.
I got Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 33554440 bytes)->3v4l.org/iUCk4
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.