Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a much updated answer (originally updated from this answer]<a href="http://php.net/manual/en/function.vsprintf.php" rel="nofollow">1</a>) to your question:</p> <pre><code>function findPermutations($arr, $arrLen, $size, $perArr = array(), $pos = 0, &amp;$found = array()) { if ($size==$pos) { //if $pos reach $size then we have found one permutation $found[] = vsprintf("%s%s|%s%s|%s%s", $perArr); return; } for ($i=0; $i&lt;$arrLen; $i++) { $perArr[$pos] = $arr[$i]; //put i'th char in current position //The recursive call that move to next position with $pos+1 findPermutations($arr, $arrLen, $size, $perArr, $pos+1, $found); } return $found; } $permutations = array(); $letters = array('1','2','3'); $max_length = 6; $permutations = findPermutations($letters, count($letters), $max_length); for($i = 0; $i &lt; count($permutations); $i++) { print ($permutations[$i].'&lt;br/&gt;'); } </code></pre> <p>Here is what I'm doing. I'm passing in an empty array called <code>$permutations</code> by reference, and as I find new permutations, I'm appending them to it. When the function <code>findPermutations()</code> is complete, I end up with an array of all permutation, that I can iterate over or insert. To get the formatting I'm using <a href="http://php.net/manual/en/function.vsprintf.php" rel="nofollow">vsprintf</a>, that lets me pass an array of data and apply a format (in this case <code>%s%s|%s%s|%s%s</code>). Lastly I'm using default argument values to make calling this function cleaner.</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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