red blue red blue green red green blue green red blue greenInitially my first impression was to use recursive function call, but it turned out to be quite complicated. After googling, Tom Butler has a very simple solution. I have modified it so that it will return an array of combinations:
public static function getQuestionCombinations($array) { $return = array(); $num = count($array); // The total number of possible combinations $total = pow(2, $num); // Loop through each possible combination for ($i = 0; $i < $total; $i++) { //For each combination check if each bit is set $data = array(); for ($j = 0; $j < $total; $j++) { // Is bit $j set in $i? if (pow(2, $j) & $i) { $data[] = $array[$j]; } } if($data) { $return[] = $data; } } return $return; }Tom has already explained in this post about the idea behind the function. This saved me heaps of time.