Getting all possible order variations of array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

Getting all possible order variations of array

Post by nutkenz »

Is there an existing function or can someone think of a good way to get all possible variations of the order of elements in an array? By this I mean, if I had an array like this:

Code: Select all

array(1,2,3,4);
I would run it through the function and get:

Code: Select all

array(
   array(1,2,3,4);
   array(1,3,2,4);
   array(1,3,4,2);
   array(1,4,2,3);
   array(1,4,3,2);
   array(2,3,4,1);
   ...
)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: Getting all possible order variations of array

Post by hawleyjr »

This can be done with a recursive loop.
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

Re: Getting all possible order variations of array

Post by nutkenz »

hawleyjr wrote:This can be done with a recursive loop.
Could you into a bit more detail? Also, would this be the most resource-friendly solution?
Post Reply