所有子数组元素无重复的组合

Combinations of all sub array elements without repeats

我有 "attributes" 来自数据库。每个属性都有很多值。现在我想混合这些值来创建独特的组合。

输入示例:

$a = array(
    35=>array('green','red','brown'),
    36=>array('fox','house','dog')
);

输出 - 二维元素的所有可能组合。下面的示例输出:

$output = array(
    array(35=>'green',36=>'fox'),
    array(35=>'green',36=>'house'),
    array(35=>'green',36=>'dog'),
    array(35=>'red',36=>'fox'),
    array(35=>'red',36=>'house'),
    array(35=>'red',36=>'dog'),
    array(35=>'brown',36=>'fox'),
    array(35=>'brown',36=>'house'),
    array(35=>'brown',36=>'dog'),
);

我的函数没有复发:

function myfunction($a){

    $keys = array_keys($a);
    $result = array();

    if(count($keys)==0){
        $result = array();
    }
    elseif(count($keys)==1){
        $k = $keys[0];
        foreach($a[$k] as $v){
            $result[] = array($k=>$v);
        }
    }
    elseif(count($keys)==2){
        $k1 = $keys[0];
        $k2 = $keys[1];
        foreach($a[$k1] as $v1){
            foreach($a[$k2] as $v2){
                $result[] = array($k1=>$v1,$k2=>$v2);
            }
        }
    }
    elseif(count($keys)==3){
        $k1 = $keys[0];
        $k2 = $keys[1];
        $k3 = $keys[2];
        foreach($a[$k1] as $v1){
            foreach($a[$k2] as $v2){
                foreach($a[$k3] as $v3){
                    $result[] = array($k1=>$v1,$k2=>$v2,$k3=>$v3);
                }
            }
        }
    }
    else{
        throw new Exception('To much keys', 1);
    }

    return $result;
}

这应该适合你:

那么这段代码有什么作用?

1。一共有多少种组合?

所以首先问题是有多少组合,答案是你必须将每个数组的数量相乘。

所以 (c = 数量1):

carray 1 * carray 2 * ... * carray n

具体针对您的示例:

carray 1 * carray 2 = 3 * 3 = 9

*1 如果你想知道为什么我选择 c 作为数量,因为函数 count() in php

2。获取所有组合

我们现在如何得到所有数组的长度的所有组合?

非常简单,我们只是遍历所有组合(开始时只是一个空组合 ([] == array())),我们已经有了下一个数组,直到我们得到我们想要的所需长度,在本例是最后一个数组的最后一次迭代。

举个例子:

Array with the elements (Empty array is '[]'):

[
    [1, 2],
    [3, 4]
]

                               //new combinations for the next iteration
                               |
array NAN*:

    Combinations:
                  - []         |  -> []
                                  |
array 1 [1,2]:       -------------
                    |             |
    Combinations:   v             v
                  - []    + 1  |  -> [1]  
                  - []    + 2  |  -> [2]   
                                  |
array 2 [3,4]:       -------------
                    |             |
    Combinations:   v             v
                  - []    + 3  |  -> [3]
                  - []    + 4  |  -> [4]
                  - [1]   + 3  |  -> [1,3]
                  - [1]   + 4  |  -> [1,4]
                  - [2]   + 3  |  -> [2,3]
                  - [2]   + 4  |  -> [2,4]     
                               //^ All combinations here

* NAN: 不是数字

因此,正如您在上面的示例中看到的,我们现在拥有所有组合,其长度为我们拥有的所有数组的数量。

但为了仅获得具有所需长度的组合,我们每次迭代都会覆盖结果数组,因此最后结果数组中只有具有预期长度的组合。

代码:

<?php

    $data = [
            35 => ["green", "red", "brown"],
            36 => ["fox", "house", "dog"]
        ];

    $combinations = [[]];
    $comKeys = array_keys($data);


    for ($count = 0; $count < count($comKeys); $count++) {
        $tmp = [];
        foreach ($combinations as $v1) {
            foreach ($data[$comKeys[$count]] as $v2)
                $tmp[] = $v1 + [$comKeys[$count] => $v2];

        }
        $combinations = $tmp;
    }

    print_r($combinations);

?>

输出:

Array
(
    [0] => Array
        (
            [35] => green
            [36] => fox
        )

    [1] => Array
        (
            [35] => green
            [36] => house
        )

    [2] => Array
        (
            [35] => green
            [36] => dog
        )

    [3] => Array
        (
            [35] => red
            [36] => fox
        )

    [4] => Array
        (
            [35] => red
            [36] => house
        )

    [5] => Array
        (
            [35] => red
            [36] => dog
        )

    [6] => Array
        (
            [35] => brown
            [36] => fox
        )

    [7] => Array
        (
            [35] => brown
            [36] => house
        )

    [8] => Array
        (
            [35] => brown
            [36] => dog
        )

)

你可以使用这个:https://gist.github.com/jwage/11193216

使用下面的代码:

<?php
$attributes = [
    [
        'P', 'M', 'G', 'XG'
    ],
    [
        'Vermelho', 'Amarelo', 'Verde'
    ],
];



function array_mix($leftItems, $rightItems)
{
    $results   = [];
    foreach ($leftItems as $leftItem) {
        foreach ($rightItems as $key => $rightItem) {
            $results[] = array_merge((array) $leftItem, (array) $rightItem);
        }
    }
    return $results;
}

$y = $attributes[0];

foreach($attributes as $key => $attrs) {
    if(isset($attributes[$key + 1]) && is_array($attributes[$key + 1])) {
        $y = array_mix($y, $attributes[$key + 1]);
    }
}

var_dump($y);