I know this question popped up alot of times here. I did search before but didn't find anything that fits.
I'm sitting here since like 5 hours and my brain is not getting me anywhere at the moment. D:
I've got an array:
[rahmenfarbe] => Array
(
[0] => Graphite
[1] => Aluminium
[2] => Smoke
)
[rueckenunterstuetzung] => Array
(
[0] => PostureFit
[1] => LumbalSupport
)
[armauflagen] => Array
(
[0] => Leder
[1] => Vinyl
)
[rollen] => Array
(
[0] => Teppichrollen
[1] => Hartbodenrollen
)
And I want to create an array which contains all possible combinations of the above (child-)options.
Example:
[0] => Array
(
[rahmenfarbe] => Graphite
[rueckenunterstuetzung] => PostureFit
[armauflagen] => Leder
[rollen] => Teppichrollen
)
[1] => Array
(
[rahmenfarbe] => Aluminium
[rueckenunterstuetzung] => PostureFit
[armauflagen] => Leder
[rollen] => Teppichrollen
)
[2] => Array
(
[rahmenfarbe] => Smoke
[rueckenunterstuetzung] => PostureFit
[armauflagen] => Leder
[rollen] => Teppichrollen
)
[3] => Array
(
[rahmenfarbe] => Graphite
[rueckenunterstuetzung] => LumbalSupport
[armauflagen] => Leder
[rollen] => Teppichrollen
)
[4] => Array
(
[rahmenfarbe] => Aluminium
[rueckenunterstuetzung] => LumbalSupport
[armauflagen] => Leder
[rollen] => Teppichrollen
)
[5] => Array
(
[rahmenfarbe] => Smoke
[rueckenunterstuetzung] => LumbalSupport
[armauflagen] => Leder
[rollen] => Teppichrollen
)
[6] => Array
(
[rahmenfarbe] => Graphite
[rueckenunterstuetzung] => PostureFit
[armauflagen] => Vinyl
[rollen] => Teppichrollen
)
Thats the code I've got so far:
$template = "test";
$maxCombinations = 0;
$optionKeys = array_keys($options);
foreach($optionKeys as $index => $optionKey) {
$indexCounters[$optionKey] = 0;
$maxCombinations += count($options[$optionKey]);
}
$maxCombinations *= count($options);
echo "Max: {$maxCombinations}\n\n";
$i1 = 0;
$i2 = 0;
while (true) {
// ** Debug Output
echo str_repeat("-", 80) . "\n";
print_r($indexCounters);
echo str_repeat("-", 80) . "\n";
// ** Debug Output
foreach ($optionKeys as $optionKey) {
$matrix[$template][$combinationsCount][$optionKey] = $options[$optionKey][$indexCounters[$optionKey]];
echo "[DEBUG] matrix[\"{$template}\"][\"{$combinationsCount}\"][\"{$optionKey}\"] = options[\"{$optionKey}\"][\"{$indexCounters[$optionKey]}\"] ({$options[$optionKey][$indexCounters[$optionKey]]})\n";
}
$combinationsCount++;
echo str_repeat("-", 80) . "\n";
$indexCounters[$optionKeys[$i1]]++;
if ($indexCounters[$optionKeys[$i1]] >= count($options[$optionKeys[$i1]])) {
$i1 = 0;
$i2++;
if ($i2 >= count($options))
break;
for ($a = 0; $a < $i2; $a++)
$indexCounters[$optionKeys[$a]] = 0;
$indexCounters[$optionKeys[$i2]]++;
}
}
print_r($matrix);
Basically it works. But it does not generate all possible combinations. From debug output I see that the counters $i1
and $i2
do not behave like I thought and thus ending the loop too early. Guess I need to figure that out tomorrow. Already too late (3:58 am) anyway ...
But maybe one of you kind guys got another bright idea how to solve that? Maybe something recursive? Thought about that but didn't attempt an implementation yet.
Any help is appreciated! :)
Thanks!