I know this question has arisen many times here. I used to search, but did not find anything suitable.
Iβm sitting here because, like 5 hours, my brain will not deliver me anywhere. D:
I have 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 that contains all the 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
)
Here is the code that I still have:
$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);
It basically works. But it does not generate all possible combinations. I see from the output of debugging that counts $i1, and $i2not behave as I thought, and thus completing the cycle too early. I guess I need to understand this tomorrow. It's too late (3:58 a.m.) anyway ...
, , , , , ? , - ? , .
!:)
!