I’m having trouble creating the recipes for a multi-variate test. For example, if I wanted to test a combination of outfits and I had 3 different hats, shirts, and pants. I want to list every possible combination of them without duplicates. This is my thought process so far:
// outfit #1
$outfit[0][0] = "hat A ";
$outfit[0][1] = "shirt A ";
$outfit[0][2] = "pants A ";
// outfit #2
$outfit[0][0] = "hat B ";
$outfit[0][1] = "shirt B ";
$outfit[0][2] = "pants B ";
// outfit #3
$outfit[0][0] = "hat C ";
$outfit[0][1] = "shirt C ";
$outfit[0][2] = "pants C ";
function recipeMaker()
{
$i = 0;
$j = 0;
foreach ($outfit as $outfit_2)
{
foreach ($outfit_2 as $outfit_3)
{
...some magic here...
recipe[$i][$j] = ...something goes here...
$j++;
}
$i++;
}
}
foreach ($recipe as $r)
{
echo $r . "<br />";
}
Then it should output:
hat A shirt A pants A
hat B shirt A pants A
hat C shirt A pants A
hat A shirt B pants A
etc.
You could go down the route of nesting
foreachloops, but what happens when you want to extend the outfit (e.g. add a list of ties)? Here’s a solution which outputs the combinations available from any number of collections:It moves along the list of types and picks one (say Hat A) then recursively builds the combinations of the rest of the types that go with that item. To add a new type it’s as simple as passing another argument to the constructor.