PHP algorithm to generate all combinations of a certain size from a single collection


Asim Isaac

I'm trying to derive an algorithm that generates all possible combinations of a certain size, such as a function that takes an array of chars and size as its arguments, and returns an array of combinations.

Example: Suppose we have a set of characters: Set A = {A,B,C}

a) All possible combinations of size 2: (3^2 = 9)

AA, AB, AC
BA, BB, BC
CA, CB, CC

b) All possible combinations of size 3: (3^3 = 27)

AAA, AAB, AAC,
ABA, ABB, ACC,
CAA, BAA, BAC,
.... ad so on total combinations = 27

Note that the size of a wire pair can be larger than the total size of the peers. predecessor. If the set contains 3 characters, we can also create combinations of size 4.

Edit : Also note that this is not the same as permutation. In permutation, we cannot have repeated characters, for example, AA will not appear if permutation algorithm is used. In statistics, this is called sampling.

Joel Hinz

I would use recursive functions. Here is an annotated (working) example. Hope this works for you!

function sampling($chars, $size, $combinations = array()) {

    # if it's the first iteration, the first set 
    # of combinations is the same as the set of characters
    if (empty($combinations)) {
        $combinations = $chars;
    }

    # we're done if we're at size 1
    if ($size == 1) {
        return $combinations;
    }

    # initialise array to put new values in
    $new_combinations = array();

    # loop through existing combinations and character set to create strings
    foreach ($combinations as $combination) {
        foreach ($chars as $char) {
            $new_combinations[] = $combination . $char;
        }
    }

    # call same function again for the next iteration
    return sampling($chars, $size - 1, $new_combinations);

}

// example
$chars = array('a', 'b', 'c');
$output = sampling($chars, 2);
var_dump($output);
/*
array(9) {
  [0]=>
  string(2) "aa"
  [1]=>
  string(2) "ab"
  [2]=>
  string(2) "ac"
  [3]=>
  string(2) "ba"
  [4]=>
  string(2) "bb"
  [5]=>
  string(2) "bc"
  [6]=>
  string(2) "ca"
  [7]=>
  string(2) "cb"
  [8]=>
  string(2) "cc"
}
*/

Related


php generate all combinations from a given array

Larry 13 What is the easiest way to convert this PHParray $a = array('A' => array(1, 2), 'B' => array(3, 4), 'C' => array(5)); to this: $result = array( array('A' => 1, 'B' => 3, 'C' => 5), array('A' => 1, 'B' => 4, 'C' => 5),

php generate all combinations from a given array

Larry 13 What is the easiest way to convert this PHParray $a = array('A' => array(1, 2), 'B' => array(3, 4), 'C' => array(5)); to this: $result = array( array('A' => 1, 'B' => 3, 'C' => 5), array('A' => 1, 'B' => 4, 'C' => 5),

Algorithm to generate all combinations of strings

John: I found a link online that shows an algorithm to generate all combinations of strings : http://www.mytechinterviews.com/combinations-of-a-string The algorithm is reproduced as follows. void combine(String instr, StringBuffer outstr, int index) { for

Algorithm to generate all combinations of strings

John: I found a link online that shows an algorithm to generate all combinations of strings : http://www.mytechinterviews.com/combinations-of-a-string The algorithm is reproduced as follows. void combine(String instr, StringBuffer outstr, int index) { for

Algorithm to generate all combinations of strings

John: I found a link online that shows an algorithm to generate all combinations of strings : http://www.mytechinterviews.com/combinations-of-a-string The algorithm is reproduced as follows. void combine(String instr, StringBuffer outstr, int index) { for

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now I'm trying to write a function that takes an array and an integer n and gives a list of each combination of size n (i.e. a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I don'

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now, I'm trying to write a function that takes an array and an integer n, and gives a list of each combination of size n (hence a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I c

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now, I'm trying to write a function that takes an array and an integer n, and gives a list of each combination of size n (hence a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I c

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now I'm trying to write a function that takes an array and an integer n and gives a list of each combination of size n (i.e. a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I don'

Algorithm to generate n combinations of size k characters

alex_and_ra I need an algorithm that generates all combinations of size n of k characters. For example, if I have n=1 and k={a,b}, the result should be: a b If n=3 and k={a,b}, the result should be: a a a a a b a b a a b b b a a b a b b b a b b b Can someone

Algorithm to generate n combinations of size k characters

alex_and_ra I need an algorithm that generates all combinations of size n of k characters. For example, if I have n=1 and k={a,b}, the result should be: a b If n=3 and k={a,b}, the result should be: a a a a a b a b a a b b b a a b a b b b a b b b Can someone

Generate all possible combinations in PHP

Antoine I have an underlined sequence of numbers: _10_1_18_4_9_14_ _ I want to replace underscores with letters without touching numbers and generate a full list of combinations, but I don't know how to do it. Right now I have this: $alph = array("A","B","C","

Generate all possible combinations in PHP

Antoine I have an underlined sequence of numbers: _10_1_18_4_9_14_ _ I want to replace underscores with letters without touching numbers and generate a full list of combinations, but I don't know how to do it. Right now I have this: $alph = array("A","B","C","

Generate all possible combinations in PHP

Antoine I have an underlined sequence of numbers: _10_1_18_4_9_14_ _ I want to replace underscores with letters without touching numbers and generate a full list of combinations, but I don't know how to do it. Right now I have this: $alph = array("A","B","C","

Get the average of all combinations of a certain size in a column

Chagag I have a dataframe with the following values: data= structure(list(rating = c(5L, 1L, 7L, 6L, 2L, 2L)), row.names = c(NA, -6L), class = "data.frame") I have 20 different options to choose 3 values from the "rating" column (3 out of 6). I want to creat

Get the average of all combinations of a certain size in a column

Chagag I have a dataframe with the following values: data= structure(list(rating = c(5L, 1L, 7L, 6L, 2L, 2L)), row.names = c(NA, -6L), class = "data.frame") I have 20 different options to choose 3 values from the "rating" column (3 out of 6). I want to creat

Get the average of all combinations of a certain size in a column

Chagag I have a dataframe with the following values: data= structure(list(rating = c(5L, 1L, 7L, 6L, 2L, 2L)), row.names = c(NA, -6L), class = "data.frame") I have 20 different options to choose 3 values from the "rating" column (3 out of 6). I want to creat

Get the average of all combinations of a certain size in a column

Chagag I have a dataframe with the following values: data= structure(list(rating = c(5L, 1L, 7L, 6L, 2L, 2L)), row.names = c(NA, -6L), class = "data.frame") I have 20 different options to choose 3 values from the "rating" column (3 out of 6). I want to creat

Generate all possible combinations of dynamic size d?

Jarvis I want to make d dynamic here, i.e. I want to be able to generate all possible combinations of array values without knowing the value of d in advance. Right now, I'm using an if clause and I can only support d to 1 to 4. These are the input parameters:

Generate all possible combinations of dynamic size d?

Jarvis I want to make d dynamic here, i.e. I want to be able to generate all possible combinations of array values without knowing the value of d in advance. Right now, I'm using an if clause and I can only support d to 1 to 4. These are the input parameters:

Generate all possible combinations of dynamic size d?

Jarvis I want to make d dynamic here, i.e. I want to be able to generate all possible combinations of array values without knowing the value of d in advance. Right now, I'm using an if clause and I can only support d to 1 to 4. These are the input parameters:

PHP - Generate all combinations of items in an array

Battle Star 20 I have a PHP array like this... $myarray = array( 'red', 'yellow', 'green, 'blue' ); The order doesn't matter to me, so I guess I'm trying to calculate combinationsinstead permutations. I'd like to get this back... $finalarray = array( 'Red