Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Solution</strong></p> <pre><code>function combinationsByLenth($arr, $len) { $combinations = []; $select = array_fill(0, $len, 0); $selectCount = count($select); $arrCount = count($arr); $possibleCombinations = pow($arrCount, $selectCount); while ($possibleCombinations-- &gt; 0) { $combination = ''; foreach ($select as $index) { $combination .= $arr[$index]; } $combinations[] = $combination; for ($i = $selectCount - 1; $i &gt;= 0; $i--) { if ($select[$i] !== ($arrCount - 1)) { $select[$i]++; break; } else { $select[$i] = 0; } } } return $combinations; } function combinationsByMinMax($arr, $min, $max) { $combinations = []; for ($i = $min; $i &lt;= $max; $i++) { $combinations = array_merge($combinations, combinationsByLenth($arr, $i)); } return $combinations; } print_r(combinationsByMinMax($arr, 1, 5)); </code></pre> <p><strong>Output</strong></p> <pre><code>Array ( [0] =&gt; a [1] =&gt; b [2] =&gt; c [3] =&gt; 1 [4] =&gt; 2 [5] =&gt; 3 [6] =&gt; aa [7] =&gt; ab [8] =&gt; ac [9] =&gt; a1 ... [9320] =&gt; 3332c [9321] =&gt; 33321 [9322] =&gt; 33322 [9323] =&gt; 33323 [9324] =&gt; 3333a [9325] =&gt; 3333b [9326] =&gt; 3333c [9327] =&gt; 33331 [9328] =&gt; 33332 [9329] =&gt; 33333 ) </code></pre> <p><strong>Rationale</strong></p> <ul> <li>Avoid recursive solutions for this type of problem in PHP (note, I like recursion in languages optimized to handle it better) to avoid stack issues as the length grows.</li> <li>Break the function up into two separate functions, one that implements min and max length, and one that gets the possible combinations for a specific length to promote reuse and clarity.</li> <li>Limit function calls within the 2 functions as much as possible to enhance performance (this algorithm gets heavy fast!)</li> </ul> <p><strong>How I Developed This Solution</strong></p> <p>I started out with a function that handled the specific case of combinations of length 5, refactored to generalize the algorithm, refactored to generalize the algorithm, etc. I've put up a quick gist so you can see the iterations to get to this working version as a case study of how to approach building this type of algorithm:</p> <p><a href="https://gist.github.com/AdamJonR/5278480" rel="nofollow">https://gist.github.com/AdamJonR/5278480</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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