PHP Find All (somewhat) Unique Combinations of an Array

后端 未结 6 2203
独厮守ぢ
独厮守ぢ 2020-12-03 12:02

I\'ve been looking at PHP array permutation / combination questions all day.. and still can\'t figure it out :/

If I have an array like:

20 //key bei         


        
6条回答
  •  北海茫月
    2020-12-03 12:33

    The Idea is simple. Suppose you know how to permute, then if you save these permutations in a set it becomes a combinations. Set by definition takes care of the duplicate values. The Php euqivalent of Set or HashSet is SplObjectStorage and ArrayList is Array. It should not be hard to rewrite. I have an implementation in Java:

    public static HashSet> permuteWithoutDuplicate(ArrayList input){
              if(input.size()==1){
                  HashSet> b=new HashSet>();
                  b.add(input);
                  return b;
              }
              HashSet>ret= new HashSet>();
              int len=input.size();
              for(int i=0;i>temp=permuteWithoutDuplicate(new ArrayList(input));
                  for(ArrayList t:temp)
                      t.add(a);
                  ret.addAll(temp);
                  input.add(i, a);
              }
              return ret;
          }
    

提交回复
热议问题