PHP Find All (somewhat) Unique Combinations of an Array

后端 未结 6 2166
独厮守ぢ
独厮守ぢ 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:30

    Had the same problem and found a different and bitwise, faster solution:

    function bitprint($u) {
        $s = array();
        for ($n=0; $u; $n++, $u >>= 1){
            if ($u&1){
                $s [] = $n;
            }
        }
        return $s;
    }
    function bitcount($u) {
        for ($n=0; $u; $n++, $u = $u&($u-1));
        return $n;
    }
    function comb($c,$n) {
        $s = array();
        for ($u=0; $u<1<<$n; $u++){
            if (bitcount($u) == $c){
                $s [] = bitprint($u);
            }
        }
        return $s;
    }
    

    This one generates all size m combinations of the integers from 0 to n-1, so for example m = 2, n = 3 and calling comb(2, 3) will produce:

    0 1
    0 2
    1 2
    

    It gives you index positions, so it's easy to point to array elements by index.

    Edit: Fails with input comb(30, 5). Have no idea why, anyone any idea?

    0 讨论(0)
  • 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<ArrayList<Integer>> permuteWithoutDuplicate(ArrayList<Integer> input){
              if(input.size()==1){
                  HashSet<ArrayList<Integer>> b=new HashSet<ArrayList<Integer>>();
                  b.add(input);
                  return b;
              }
              HashSet<ArrayList<Integer>>ret= new HashSet<ArrayList<Integer>>();
              int len=input.size();
              for(int i=0;i<len;i++){
                  Integer a = input.remove(i);
                  HashSet<ArrayList<Integer>>temp=permuteWithoutDuplicate(new ArrayList<Integer>(input));
                  for(ArrayList<Integer> t:temp)
                      t.add(a);
                  ret.addAll(temp);
                  input.add(i, a);
              }
              return ret;
          }
    
    0 讨论(0)
  • 2020-12-03 12:33

    Cleaned up Adi Bradfield's sugestion using strrev and for/foreach loops, and only get unique results.

    function search_get_combos($array = array()) {
    sort($array);
    $terms = array();
    
    for ($dec = 1; $dec < pow(2, count($array)); $dec++) {
        $curterm = array();
        foreach (str_split(strrev(decbin($dec))) as $i => $bit) {
            if ($bit) {
                $curterm[] = $array[$i];
            }
        }
        if (!in_array($curterm, $terms)) {
            $terms[] = $curterm;
        }
    }
    
    return $terms;
    }
    
    0 讨论(0)
  • 2020-12-03 12:39

    If you don't mind using a couple of global variables, you could do this in PHP (translated from a version in JavaScript):

    <?PHP
    $result = array(); 
    $combination = array();
    
    function combinations(array $myArray, $choose) {
      global $result, $combination;
    
      $n = count($myArray);
    
      function inner ($start, $choose_, $arr, $n) {
        global $result, $combination;
    
        if ($choose_ == 0) array_push($result,$combination);
        else for ($i = $start; $i <= $n - $choose_; ++$i) {
               array_push($combination, $arr[$i]);
               inner($i + 1, $choose_ - 1, $arr, $n);
               array_pop($combination);
             }
      }
      inner(0, $choose, $myArray, $n);
      return $result;
    }
    
    print_r(combinations(array(20,20,22,24), 3));
    ?>
    

    OUTPUT:

    Array ( [0] => Array ( [0] => 20 
                           [1] => 20 
                           [2] => 22 ) 
            [1] => Array ( [0] => 20 
                           [1] => 20 
                           [2] => 24 ) 
            [2] => Array ( [0] => 20 
                           [1] => 22 
                           [2] => 24 ) 
            [3] => Array ( [0] => 20 
                           [1] => 22 
                           [2] => 24 ) ) 
    
    0 讨论(0)
  • 2020-12-03 12:43

    The pear package Math_Combinatorics makes this kind of problem fairly easy. It takes relatively little code, it's simple and straightforward, and it's pretty easy to read.

    $ cat code/php/test.php
    <?php
    $input = array(20, 20, 22, 24);
    
    require_once 'Math/Combinatorics.php';
    
    $c = new Math_Combinatorics;
    $combinations = $c->combinations($input, 3);
    for ($i = 0; $i < count($combinations); $i++) {
      $vals = array_values($combinations[$i]);
      $s = implode($vals, ", ");
      print $s . "\n";
    }
    ?>
    
    $ php code/php/test.php
    20, 20, 22
    20, 20, 24
    20, 22, 24
    20, 22, 24
    

    If I had to package this as a function, I'd do something like this.

    function combinations($arr, $num_at_a_time) 
    {
        include_once 'Math/Combinatorics.php';
    
        if (count($arr) < $num_at_a_time) {
            $arr_count = count($arr);
            trigger_error(
                "Cannot take $arr_count elements $num_at_a_time " 
                ."at a time.", E_USER_ERROR
            );
        }
    
        $c = new Math_Combinatorics;
        $combinations = $c->combinations($arr, $num_at_a_time);
    
        $return = array();
        for ($i = 0; $i < count($combinations); $i++) {
            $values = array_values($combinations[$i]);
            $return[$i] = $values;
        }
        return $return;
    }
    

    That will return an array of arrays. To get the text . . .

    <?php
      include_once('combinations.php');
    
      $input = array(20, 20, 22, 24);
      $output = combinations($input, 3);
    
      foreach ($output as $row) {
          print implode($row, ", ").PHP_EOL;
      }
    ?>
    20, 20, 22
    20, 20, 24
    20, 22, 24
    20, 22, 24
    
    0 讨论(0)
  • 2020-12-03 12:45

    Why not just use binary? At least then its simple and very easy to understand what each line of code does like this? Here's a function i wrote for myself in a project which i think is pretty neat!

    function search_get_combos($array){
    $bits = count($array); //bits of binary number equal to number of words in query;
    //Convert decimal number to binary with set number of bits, and split into array
    $dec = 1;
    $binary = str_split(str_pad(decbin($dec), $bits, '0', STR_PAD_LEFT));
    while($dec < pow(2, $bits)) {
        //Each 'word' is linked to a bit of the binary number.
        //Whenever the bit is '1' its added to the current term.
        $curterm = "";
        $i = 0;
        while($i < ($bits)){
            if($binary[$i] == 1) {
                $curterm[] = $array[$i]." ";
            }
            $i++;
        }
        $terms[] = $curterm;
        //Count up by 1
        $dec++;
        $binary = str_split(str_pad(decbin($dec), $bits, '0', STR_PAD_LEFT));
    }
    return $terms;
    } 
    

    For your example, this outputs:

    Array
    (
        [0] => Array
            (
                [0] => 24 
            )
        [1] => Array
            (
                [0] => 22 
            )
        [2] => Array
            (
                [0] => 22 
                [1] => 24 
            )
        [3] => Array
            (
                [0] => 20 
            )
        [4] => Array
            (
                [0] => 20 
                [1] => 24 
            )
        [5] => Array
            (
                [0] => 20 
                [1] => 22 
            )
        [6] => Array
            (
                [0] => 20 
                [1] => 22 
                [2] => 24 
            )
        [7] => Array
            (
                [0] => 20 
            )
        [8] => Array
            (
                [0] => 20 
                [1] => 24 
            )
        [9] => Array
            (
                [0] => 20 
                [1] => 22 
            )
        [10] => Array
            (
                [0] => 20 
                [1] => 22 
                [2] => 24 
            )
        [11] => Array
            (
                [0] => 20 
                [1] => 20 
            )
        [12] => Array
            (
                [0] => 20 
                [1] => 20 
                [2] => 24 
            )
        [13] => Array
            (
                [0] => 20 
                [1] => 20 
                [2] => 22 
            )
        [14] => Array
            (
                [0] => 20 
                [1] => 20 
                [2] => 22 
                [3] => 24 
            )
    )
    
    0 讨论(0)
提交回复
热议问题