Page 1 of 1

Question regarding combinations of elements

Posted: Wed Jan 28, 2009 4:25 am
by sheridanhu
Hi everybody,

Great that I found this place. I wonder if you guys could help me with this issue:

I have a number of elements, for the sake of simplicity let's call them: AAA, BBB, CCC, DDD and EEE. In reality, there may be hundred of these bastards but here let's take only five...

They are put in an array, like so:

Code: Select all

 
$elements=array("AAA","BBB","CCC","DDD","EEE");
 
These elements are supposed to be put into a multi-dimensional array, which must contain any and all combinations of these elements put beside one-other. Let me explain:

Code: Select all

 
$combinations=array(
     array=("AAA","BBB","CCC","DDD","EEE"),
     array=("BBB","EEE","AAA","CCC","DDD"),
     array=("AAA","AAA","CCC","EEE","EEE"),
     );
 
So, every "row" you can see in the above array must contain an array which contains one unique combination of the above elements. The combination may contain all or only some of the elements, one element may be repeated several times or may be the only element being repeated, but in the end the "row" must always be five "elements long".

Obviously the job is to write up all any any possible combinations of these elements into this $combinations array. How do you think would be the most effective way to do this?

Thanks for any help... :)