douyue8191
2017-09-21 14:28
浏览 70
已采纳

php从多维数组中获取所有有效组合

I have a array which represent allowed value in a key. The length of this array is dynamic

array(
 10=>array(100,101,102),
 11=>array(100,104),
 12=>array(100,102,103)
)

in this exemple, key 10 can have 100, 101 and 102.

I want to get all valid combination where each value appear only one time

array(
   array(10=>array(100,101,102),11=>array(104),12=>array(103)),
   array(10=>array(100,101),10=>array(104),12=>array(102,103)),
   array(10=>array(101),10=>array(100,104),12=>array(102,103)),
   array(10=>array(101),10=>array(104),12=>array(100,102,103)),
   AND SO ON
)

图片转代码服务由CSDN问答提供 功能建议

我有一个数组,表示一个键中的允许值。 该数组的长度是动态的

 数组(
 10 =>数组(100,101,102),
 11 =>数组(100,104),
 12 =>  ;数组(100,102,103)
)
   
 
 

在这个例子中,键10可以有100,101和102.

我希望获得所有有效组合,其中每个值只出现一次

 数组(
数组(10 =>数组(100,101,102),11 =>数组(104)  ),12 =>数组(103)),
数组(10 =>数组(100,101),10 =>数组(104),12 =>数组(102,103)),
数组(10 =  > array(101),10 => array(100,104),12 => array(102,103)),
 array(10 => array(101),10 => array(104),12 =  > array(100,102,103)),
 AND SO ON 
)
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongni3854 2017-09-21 17:34
    已采纳

    They want you to find the power sets for each index and then calculate the power set of the sets.

    This code will allows you to calculate the power set for each index. It should get you started and give you an idea of how to complete the rest of the problem.

    function power_set($arr)
    {
        $sets = [];
    
        for ($i = 0; $i < pow(2, count($arr)); $i++) {
            $set = [];
            for ($j = 0; $j < count($arr); $j++) {
                if ($i & (1 << $j)) {
                    $set[] = $arr[$j];
                }
            }
            if (count($set) > 0) {
                $sets[] = $set;
            }
            $set = [];
        }
        return $sets;
    }
    

    Output

    array(7) {
      [0] =>
      array(1) {
        [0] =>
        int(100)
      }
      [1] =>
      array(1) {
        [0] =>
        int(101)
      }
      [2] =>
      array(2) {
        [0] =>
        int(100)
        [1] =>
        int(101)
      }
      [3] =>
      array(1) {
        [0] =>
        int(102)
      }
      [4] =>
      array(2) {
        [0] =>
        int(100)
        [1] =>
        int(102)
      }
      [5] =>
      array(2) {
        [0] =>
        int(101)
        [1] =>
        int(102)
      }
      [6] =>
      array(3) {
        [0] =>
        int(100)
        [1] =>
        int(101)
        [2] =>
        int(102)
      }
    }
    

    One thing that I did notice, is that you aren't including the case where the set has no elements. Technically that should be included, so I would check to ensure that your sample output is right. I modified the algorithm to exclude this case, but I would assume that it should be included.

    If you want to include it remove the if statement that checks for the number of elements in the $set.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题