创建数组子级的所有组合 [英] Create all combinations of array children

查看:68
本文介绍了创建数组子级的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题在这里经常出现.我之前进行过搜索,但没有找到合适的内容.

I know this question popped up alot of times here. I did search before but didn't find anything that fits.

我从这里坐了大约5个小时,而现在我的大脑还无法到达任何地方. D:

I'm sitting here since like 5 hours and my brain is not getting me anywhere at the moment. 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
)

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

And I want to create an array which contains all possible combinations of the above (child-)options.

示例:

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

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

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.

感谢您的帮助! :)

谢谢!

推荐答案

这是您正在尝试做的,在php i中使用array的Permutation. e Cartesian数组的乘积.

This is you are trying to do , use Permutation of array in php i. e Cartesian product of array.

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

这篇关于创建数组子级的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆