Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Find All (somewhat) Unique Combinations of an Array
    text
    copied!<p>I've been looking at PHP array permutation / combination questions all day.. and still can't figure it out :/</p> <p>If I have an array like:</p> <pre><code>20 //key being 0 20 //key being 1 22 //key being 2 24 //key being 3 </code></pre> <p>I need combinations like:</p> <pre><code>20, 20, 22 //keys being 0 1 2 20, 20, 24 //keys being 0 1 3 20, 22, 24 //keys being 0 2 3 20, 22, 24 //keys being 1 2 3 </code></pre> <p>The code I currently have gives me:</p> <pre><code>20, 22, 24 </code></pre> <p>because it doesn't want to repeat 20... but that's what I need!</p> <p>Here is the code I have. it is directly from <a href="https://stackoverflow.com/questions/4279722/php-recursion-to-get-all-possibilities-of-strings/8880362#8880362">Php recursion to get all possibilities of strings</a></p> <pre><code>function getCombinations($base,$n){ $baselen = count($base); if($baselen == 0){ return; } if($n == 1){ $return = array(); foreach($base as $b){ $return[] = array($b); } return $return; }else{ //get one level lower combinations $oneLevelLower = getCombinations($base,$n-1); //for every one level lower combinations add one element to them that the last element of a combination is preceeded by the element which follows it in base array if there is none, does not add $newCombs = array(); foreach($oneLevelLower as $oll){ $lastEl = $oll[$n-2]; $found = false; foreach($base as $key =&gt; $b){ if($b == $lastEl){ $found = true; continue; //last element found } if($found == true){ //add to combinations with last element if($key &lt; $baselen){ $tmp = $oll; $newCombination = array_slice($tmp,0); $newCombination[]=$b; $newCombs[] = array_slice($newCombination,0); } } } } } return $newCombs; } </code></pre> <p>I've been playing around with the <code>($b == $lastEl)</code> line, with no luck</p> <p>===============</p> <p>Questions I've already looked at, and are not the same OR that created an out of memory error!:</p> <ul> <li><a href="https://stackoverflow.com/questions/12238675/how-can-i-get-all-permutations-in-php-without-sequential-duplicates">How can I get all permutations in PHP without sequential duplicates?</a></li> <li><a href="https://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers">Permutations - all possible sets of numbers</a></li> <li><a href="https://stackoverflow.com/questions/1679605/combinations-dispositions-and-permutations-in-php">Combinations, Dispositions and Permutations in PHP</a></li> <li><a href="https://stackoverflow.com/questions/3742506/php-array-combinations">PHP array combinations</a></li> <li><a href="https://stackoverflow.com/questions/10222835/get-all-combinations-of-a-php-array">Get all permutations of a PHP array?</a></li> <li><a href="https://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array">PHP: How to get all possible combinations of 1D array?</a></li> <li><a href="https://stackoverflow.com/questions/11340450/select-only-unique-array-values-from-this-array">Select only unique array values from this array</a></li> <li><a href="https://stackoverflow.com/questions/10222835/get-all-combinations-of-a-php-array">Get all permutations of a PHP array?</a></li> <li><a href="https://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array">PHP: How to get all possible combinations of 1D array?</a></li> <li><a href="https://stackoverflow.com/questions/11340450/select-only-unique-array-values-from-this-array">Select only unique array values from this array</a></li> <li><a href="https://stackoverflow.com/questions/12238675/how-can-i-get-all-permutations-in-php-without-sequential-duplicates">How can I get all permutations in PHP without sequential duplicates?</a></li> <li><a href="https://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n">Algorithm to return all combinations of k elements from n</a></li> <li><a href="https://stackoverflow.com/questions/12837431/find-combinations-sum-of-elements-in-array-whose-sum-equal-to-a-given-number">Find combination(s) sum of element(s) in array whose sum equal to a given number</a></li> <li><a href="https://stackoverflow.com/questions/1679605/combinations-dispositions-and-permutations-in-php">Combinations, Dispositions and Permutations in PHP</a></li> <li><a href="https://stackoverflow.com/questions/3742506/php-array-combinations">PHP array combinations</a></li> <li><a href="https://stackoverflow.com/questions/4279722/php-recursion-to-get-all-possibilities-of-strings/8880362#8880362">Php recursion to get all possibilities of strings</a></li> <li><a href="https://stackoverflow.com/questions/5167726/how-to-return-permutations-of-an-array-in-php">How to return permutations of an array in PHP?</a></li> <li><a href="https://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers">Permutations - all possible sets of numbers</a></li> <li><a href="https://stackoverflow.com/questions/7416401/subset-sum-problem-in-php-with-mysql">Subset-sum problem in PHP with MySQL</a></li> <li><a href="https://stackoverflow.com/questions/9088963/find-unique-combinations-of-values-from-arrays-filtering-out-any-duplicate-pairs">Find unique combinations of values from arrays filtering out any duplicate pairs</a></li> <li><a href="https://stackoverflow.com/questions/9217839/finding-all-the-unique-permutations-of-the-string">Finding all the unique permutations of a string without generating duplicates</a></li> <li><a href="https://stackoverflow.com/questions/9415692/generate-all-unique-permutations">Generate all unique permutations</a></li> <li><a href="https://stackoverflow.com/questions/9839004/subset-sum-for-exactly-k-integers">Subset sum for exactly k integers?</a></li> </ul> <p>I've tried some of these algorithms with an array of 12 items, and end up running out of memory. However the algorithm that I'm currently using doesn't give me an out of memory error.... BUT.. I need those duplicates!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload