title: Generating unique combinations in PHP
body: <p>I have a requirement to generate unique combinations from the given data set (n numbers) with each combination contains r values.</p>
  
  <p>Basically looking to implement <code>C(n,r)=n!(r!(n−r)!)</code> formula in PHP.</p>
  
  <p>Input data set {A,B,C,D} and need an unique combination of 3 values like below:</p>
  
  <pre><code>C(n,r)=C(4,3) = 4!/(3!(4−3)!)
  = 4
  </code></pre>
  
  <p>ABC
  ACD
  BCD
  BDA</p>
  
  <p>(CDA,CAB,BCA etc are duplicates and should be truncated from output).</p>
  
  <p>But my code below</p>
  
  <pre><code>&lt;?php
  function sampling($chars, $size, $combinations = array()) {
  
      # if it's the first iteration, the first set 
      # of combinations is the same as the set of characters
      if (empty($combinations)) {
          $combinations = $chars;
      }
  
      # we're done if we're at size 1
      if ($size == 1) {
          return $combinations;
      }
  
      # initialise array to put new values in
      $new_combinations = array();
  
      # loop through existing combinations and character set to create strings
      foreach ($combinations as $combination) {
          foreach ($chars as $char) {
              if($combination != $char)
              $new_combinations[] = $combination . $char;
          }
      }
  
      # call same function again for the next iteration
      return sampling($chars, $size - 1, $new_combinations);
  
  }
  ?&gt;
  </code></pre>
  
  <p>returns below 64 arrays with duplicates</p>
  
  <blockquote>
    <p>{ [0]=> string(3) "aaa" [1]=> string(3) "aab" [2]=> string(3) "aac"
    [3]=> string(3) "aad" [4]=> string(3) "aba" [5]=> string(3) "abb"
    [6]=> string(3) "abc" [7]=> string(3) "abd" [8]=> string(3) "aca"
    [9]=> string(3) "acb" [10]=> string(3) "acc" [11]=> string(3) "acd"
    [12]=> string(3) "ada" [13]=> string(3) "adb" [14]=> string(3) "adc"
    [15]=> string(3) "add" [16]=> string(3) "baa" [17]=> string(3) "bab"
    [18]=> string(3) "bac" [19]=> string(3) "bad" [20]=> string(3) "bba"
    [21]=> string(3) "bbb" [22]=> string(3) "bbc" [23]=> string(3) "bbd"
    [24]=> string(3) "bca" [25]=> string(3) "bcb" [26]=> string(3) "bcc"
    [27]=> string(3) "bcd" [28]=> string(3) "bda" [29]=> string(3) "bdb"
    [30]=> string(3) "bdc" [31]=> string(3) "bdd" [32]=> string(3) "caa"
    [33]=> string(3) "cab" [34]=> string(3) "cac" [35]=> string(3) "cad"
    [36]=> string(3) "cba" [37]=> string(3) "cbb" [38]=> string(3) "cbc"
    [39]=> string(3) "cbd" [40]=> string(3) "cca" [41]=> string(3) "ccb"
    [42]=> string(3) "ccc" [43]=> string(3) "ccd" [44]=> string(3) "cda"
    [45]=> string(3) "cdb" [46]=> string(3) "cdc" [47]=> string(3) "cdd"
    [48]=> string(3) "daa" [49]=> string(3) "dab" [50]=> string(3) "dac"
    [51]=> string(3) "dad" [52]=> string(3) "dba" [53]=> string(3) "dbb"
    [54]=> string(3) "dbc" [55]=> string(3) "dbd" [56]=> string(3) "dca"
    [57]=> string(3) "dcb" [58]=> string(3) "dcc" [59]=> string(3) "dcd"
    [60]=> string(3) "dda" [61]=> string(3) "ddb" [62]=> string(3) "ddc"
    [63]=> string(3) "ddd" }</p>
  </blockquote>
  
  <p>Thanks in advance !!!</p>