php - Find every possible permutation of array values
Situation: there are 5 questions with multiple possible answers. Four of the questions can have a single answer with one having one or more answers. Any of the questions could have no answer.
I want to work out each and every possible combination of these answers.
I think this isn't a duplicate of this as it deals with possible permutations of single characters or numbers.
I believe this example would generate something like 230,400 possible permutations
$questions = array(
"q1" => array(
"1a",
"1b",
"1c"
),
"q2" => array(
"2a",
"2b",
"2c",
"2d"
),
"q3" => array(
"3a",
"3b",
"3c"
),
"q4" => array( // this question can have any number of these answers selected, or none
"4a",
"4b",
"4c",
"4d",
"4e",
"4f"
),
"q5" => array(
"5a",
"5b",
"5c"
)
);
Answer
Solution:
I hope I've got your question right and that this answer seems helpful to you...
Initial conditions
In addition to your example, let's introduce a second array with information about which question(s) may have multiple answers:
This would tell our algorithm described in the following that question 4 may have any number of answers selected, while all over questions may only have a single or no answer at all.
Number of possible combinations of answers
The set of answers selected for a specific question is independent from any other question. For a question with a total number of
n
possible answers and maximum of 1 selected answer, the number of possible selections for that question isn+1
. If the question allows the selection of multiple answers, there are2^n
possible combinations for that question (each answer has two options: chosen or not chosen).In your example, this leads to a total number of
4 * 5 * 4 * 2^6 * 4 = 20480
possible combinations of selected answers. This number can be computed like follows:Iterating over all possible combinations of answers
If you are not only interested in the number of answer combinations, but want to generate them all, you can use an algorithm like the following. For the questions with more than one possible answer, it encodes the set of answers given as binary number. That means, the number
001010
would represent the answer set['4c','4e']
.