创建数组子项的所有组合

Create all combinations of array children

我知道这个问题在这里出现了很多次。我之前搜索过,但没有找到合适的。

我在这里坐了大约 5 个小时,而我的大脑目前无法让我到达任何地方。 D:

我有一个数组:

[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
)

我想创建一个数组,其中包含上述(子)选项的所有可能组合。

示例:

[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
)

这就是我目前得到的代码:

$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);

基本上它有效。但它不会生成所有可能的组合。从调试输出中,我看到计数器 $i1$i2 的行为与我想象的不同,因此过早结束了循环。我想我明天需要弄清楚。已经太晚了(凌晨 3:58)...

但也许你们中的某个好心人有另一个好主意如何解决这个问题?也许递归的东西?考虑过,但还没有尝试实施。

感谢任何帮助! :)

谢谢!

这是你想要做的,在 php i 中使用数组的 Permutation。 e Cartesian 数组的乘积。

function cartesian($input) {
    $result = array();

    while (list($key, $values) = each($input)) {

        if (empty($values)) {
            continue;
        }
        if (empty($result)) {
            foreach($values as $value) {
                $result[] = array($key => $value);
            }
        }
        else {

            $append = array();

            foreach($result as &$product) {

                $product[$key] = array_shift($values);
                $copy = $product;
                foreach($values as $item) {
                    $copy[$key] = $item;
                    $append[] = $copy;
                }
                array_unshift($values, $product[$key]);
            }
            $result = array_merge($result, $append);
        }
    }

    return $result;
}
$input=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',
));

echo '<pre>';
print_r(cartesian($input));
echo '</pre>';

礼貌:@georg & CBroe