douchun5969 2016-01-16 12:25
浏览 25

生成可能单击的复选框的所有组合以添加值

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?

  • 写回答

1条回答 默认 最新

  • duanfan8699 2016-01-17 14:38
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看