0

Let's say I have 10 types of fruit, and 10 jars to put them in. What would be the best way of writing a function that creates every possibility without repeating itself?

Here is an example of what I mean with 3 dimensions instead of 10. The function could produce an array of what is in each jar for every possible combination.

[[Apple, Orange, Banana], [Apple, Banana, Orange], [Orange, Apple, Banana], [Orange, Banana, Apple], [Banana, Apple, Orange], [Banana, Orange, Apple]]

Leah Sapan
  • 3,621
  • 7
  • 33
  • 57
  • Consider looping an array of fruit and an array of jars – Mark Baker Aug 09 '13 at 22:07
  • 1
    foreach ($fruits as $fruit) { foreach ($jars as $jar) { echo $fruit, $jar; } } – Mike Aug 09 '13 at 22:07
  • I'm sorry, I should have been more specific. I have revised the question to explain the problem more clearly. – Leah Sapan Aug 09 '13 at 22:14
  • 1
    you are looking for a permutation generator: http://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers – sushain97 Aug 09 '13 at 22:19
  • I'm sorry, I'm not getting your fruit/jar combinations. How does one differ from the other since all you are changing is the order? – Mike Aug 09 '13 at 22:19
  • I'm actually trying to render a list of every possible assignment using people and jobs. I just figured the fruit jar question would be easier to post. That is why the order matters, they are actually being assigned to something else. – Leah Sapan Aug 09 '13 at 22:21
  • 1
    Thank you sushain97! I really appreciate it. – Leah Sapan Aug 09 '13 at 22:23

0 Answers0