0

I have to generate all possible combinations of four checkboxes where order does not matter. The reason the order does not matter is because I will add up the values connected to those checkboxes.

The array of categories is: ['kw1','kw2','kw3','kw4']

This means that kw1+kw2 = kw2+kw1. So this must be seen as one combination.

I've gotten pretty far, but I am failing to wrap my head around it.

First of all, the user can click between 1 and 4 checkboxes, there is no fixed amount of checked checkboxes required. This can be translated to a for loop:

for($i=1;$i<=4;$i++){

}

When only 1 checkbox can be clicked, there are 4 possible options. When 4 checkboxes are checked, only 1 option is available. When the 1st checkbox is checked, there are only 3 options left. When the 2nd checkbox is checked, only 2 options remain available. You get the drill.

I translated this to:

for($i=1;$i<=4;$i++){
    $j = 5 - $i;
    $reverse_j = 0;
    $categories = array('kw1','kw2','kw3','kw4');
    for($j;$j>0;$j--){
        $reverse_j++;
        $counter = 0;
        foreach($categories as $category){
            $counter++;
            $tmp = $categories;
            if($counter <= $i){
                $result[$i][$reverse_j][] = $tmp[0];
                unset($tmp[0]);
                $tmp = array_values($tmp);
            }
        }
        unset($categories[0]);
        $categories = array_values($categories);
    }
}

The above is one of many failed attempts to trying to generate the combinations. I've written out all the combinations, because it is not that hard, but I would really want to create code for it.

The possible combinations should be:

1 click = (1), (2), (3), (4)
2 clicks = (1-2), (1-3), (1-4), (2-3) ,(2-4), (3-4)
3 clicks = (1-2-3), (1-2-4), (1-3-4), (2-3-4) 
4 clicks = (1-2-3-4)

What am I missing? Where does my thinking go wrong?

PIDZB
  • 903
  • 1
  • 15
  • 38
  • You are looking for a powerset. Have a look at this link: http://stackoverflow.com/questions/1670862/obtaining-a-powerset-of-a-set-in-java – osanger Jan 16 '16 at 12:43
  • Thank you very much for giving me the term 'powerset'. This solved my problem. I really hate it that I couldn't come up with the answer myself... I used the following post to get the code: http://stackoverflow.com/questions/7327318/power-set-elements-of-a-certain-length – PIDZB Jan 16 '16 at 14:07

1 Answers1

0

Thanks to the comment of @dr_debug, I found the right term for what I was looking for: 'powerset'.

With this term I found the following post: Power set elements of a certain length

This gave me the following functions:

function subsets_n($arr, $k)
{
  if (count($arr) < $k) return array();
  if (count($arr) == $k) return array(0 => $arr);

  $x = array_pop($arr);
  if (is_null($x)) return array();

  return array_merge(subsets_n($arr, $k),
                     merge_into_each($x, subsets_n($arr, $k-1)) );
}

function merge_into_each($x, $arr)
{
  foreach ($arr as &$a) array_push($a, $x);
  return $arr;
}

With these functions, you can then generate an array with all possible combinations!

Community
  • 1
  • 1
PIDZB
  • 903
  • 1
  • 15
  • 38