Return combination of all values in an array..
Posted: Sat Sep 23, 2006 7:07 pm
I'm trying to figure out how to do this. I can't seem to get my head around it. Basically, lets say I have an array with 3 values, now keep in mind the array can have a variable number of values, from 1 to 12 or maybe even more.
Now, what I want the code to do, is create every possible combination of these 3 values in every possible order..
ie:
red green blue
green red blue
blue green red
blue red green etc....
Then I want it to drop one of the array elements and do the same with only 2 array values..
red green
green red
Then add that value back and drop a different one..
blue green
green blue
And so one..
blue red
red blue
So basically, if an array contains 10 values, it would create every possible combination of 10,9,8,7,6,5,4,3 and 2 length values.
Any clue how to create something like this?
Code: Select all
$colors = array('red', 'green', 'blue');ie:
red green blue
green red blue
blue green red
blue red green etc....
Then I want it to drop one of the array elements and do the same with only 2 array values..
red green
green red
Then add that value back and drop a different one..
blue green
green blue
And so one..
blue red
red blue
So basically, if an array contains 10 values, it would create every possible combination of 10,9,8,7,6,5,4,3 and 2 length values.
Any clue how to create something like this?