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?