php – 没有重复的所有子数组元素的组合

我有来自数据库的“属性”.每个属性都有许多值.现在我想混合这些值来创建独特的组合.

输入示例:

$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 = amount1):

carray 1 * carray 2 * … * carray n

并特定于您的示例:

carray 1 * carray 2= 3 * 3 = 9

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

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
        )

)
上一篇:python – 如何生成列表组合?


下一篇:python – 制作列表的所有可能组合