1. ## Trying all combinations

Hi, this is really a general programming question, I have come across it a few times in my work and not really known how to handle it. I have normally ended up just coding it manually (putting in all the combinations), but I guess there must be a better way.

Say I have an array, (0, 1, 2, 3, 4, 5) and I want to put them into a function called do_something(\$a, \$b, \$c, \$d, \$e, \$f) but I want this function to be called for every combination of numbers so do_something(0, 1, 2, 3, 4, 5), do_something(0, 1, 2, 3, 5, 4) etc.

I guess you could do it with for loops but when dealing with say 9 variables, it would be slow and I guess not the most efficient way to do it.

Many thanks

2. well... slow in the context of.... 0.003 seconds? all depends on what your function actually does.... but if your question is, can I put an undefined number of variables into a function, then to my knowledge the answer is no... you have two options - make each function variable required, or give it a default value where you don't want to necessarily provide a unique value every time.

perhaps if you copy your function in then someone could give a better answer but I can't really think how to word it any better....

3. well, say I wanted to generate all the 6 digit pandigital numbers (eg 251364, 561342 etc) , then i have been previously been doing this with 6 for loops, this takes at least half a minute to run, and it's not very nice to code either - very messy.

If I could have an array of (1, 2, 3, 4, 5, 6) and then just something which could make all the different combinations of these.

so to put this into some kind of code as an example:

Code PHP:
```<?
\$numbers = array(1, 2, 3, 4, 5, 6);

//some kind of loop to do each combination
echo \$next_combination . ", ";

?>```

the above should then echo "123456, 123465, etc" or something along those lines.

Sorry if I'm not explaining very well.

Thanks again

4. Originally Posted by wheeler
but if your question is, can I put an undefined number of variables into a function, then to my knowledge the answer is no... you have two options - make each function variable required, or give it a default value where you don't want to necessarily provide a unique value every time.
Yes, pass in your argument(s) as an array.

Yes, pass in your argument(s) as an array.
ah yes, good point

6. Originally Posted by austinplatt
well, say I wanted to generate all the 6 digit pandigital numbers (eg 251364, 561342 etc) , then i have been previously been doing this with 6 for loops, this takes at least half a minute to run, and it's not very nice to code either - very messy.
Sounds odd that the code should take a half minute to run. Most probably your code can be optimised.

In this case I guess I would just have created a recursive function, which would stop when it reached the set goal.

Originally Posted by wheeler
all depends on what your function actually does.... but if your question is, can I put an undefined number of variables into a function, then to my knowledge the answer is no... you have two options - make each function variable required, or give it a default value where you don't want to necessarily provide a unique value every time.
Actually, you can pass along a changing number of arguments to an function if you want. These functions allows you to do that func_num_args(), func_get_arg(), func_get_args()

7. Originally Posted by austinplatt
Say I have an array, (0, 1, 2, 3, 4, 5) and I want to put them into a function called do_something(\$a, \$b, \$c, \$d, \$e, \$f) but I want this function to be called for every combination of numbers so do_something(0, 1, 2, 3, 4, 5), do_something(0, 1, 2, 3, 5, 4) etc.
This is called "permutations" in maths, there are several well-known algorithms to generate them, google for "permutations alogrithms" or similar.

If you're on the hunt for the sample code, check my sig, I've just posted some.

8. This sounds similar to that of entering the numbers of a phone number in order to find what words it could form.

PHP shuffle function might help.
http://au2.php.net/manual/hk/function.shuffle.php

and range http://au.php.net/manual/hk/function.range.php

This is the shuffle example in action http://www.lionslair.net.au/test/shuffle.php

Some information on permutations

http://www.mathsrevision.net/alevel/...mbinations.php

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•