I'm working out an algorithm to get permutations like
123
132
213
231
312
321
I'm doing it using nested foreach loops.
for (..) {
for(..) {
for(..) {
echo $i . $j . $k . "<br />";
}
}
}
Problem is those # of nested loops are optimized for 3-spot permutations. How can I could I dynamically set the number of nested for loops to generate 4-letter or 5-letter permutations?
Yes, just do it recursively.
Recursive method is the way to go. But you can also use
eval
function like:No, no, please do NOT use recursion for generating permutations. Use the algorithm outlined here, see also php implementation
You could use a recursive function. Take a look at this post.
The answer is: use a recursive function.