创建数组子项的所有组合

时间:2015-01-25 03:04:06

标签: php multidimensional-array permutation

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

我坐在这里就像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分)......

但也许你们中的一个人有另一个好主意如何解决这个问题?也许是递归的东西?考虑到这一点但尚未尝试实施。

任何帮助表示赞赏! :)

谢谢!

1 个答案:

答案 0 :(得分:3)

这是你要做的,在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&amp; CBroe