如何从多个数组中获取所有组合?

Posted

技术标签:

【中文标题】如何从多个数组中获取所有组合?【英文标题】:How to get all combinations from multiple arrays? 【发布时间】:2015-08-11 07:35:15 【问题描述】:

假设我有这 3 个数组

$array1 = array(1,2);
$array2 = array(4,5);
$array3 = array(7,8);

我需要这个输出

1 4 7
1 4 8
1 5 7
1 5 8
2 4 7
2 4 8
2 5 7
2 5 8

我的一个问题是我的数组可能有 3 到 15 个不同的数组,每个数组可能是空的(我可能会添加一个 0 只是为了不为空)或有很多值。 如果我有一个空数组,我还需要将其计为有效列。这些值将用于按特定顺序填充数据库。

有什么办法可以做到吗?

【问题讨论】:

我为每个数组尝试了一个 foreach() 级联,但没有奏效 让我们看看。在此处发布。 我能做的最好的事情就是从这样的东西开始 foreach ($array1 as $a1) $var .= $a1; foreach ($array2 as $a2) $var .= $a2; foreach ($array3 as $a3) $var .= $a3; $var .= '';但我不知道如何用我需要的值填充第一列 那么这个问题我们在哪里? @Phil_1984_ 但他的代码仍然不会很灵活.. 【参考方案1】:

有多少种组合?

那么首先的问题是有多少种组合?答案是你必须将每个数组的数量相乘。

所以(c = 数量1):

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

具体到你的例子:

c数组 1 * c数组 2 * c数组 3 = 2 * 2 * 2 = 8

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

将所有组合放在一起

我们现在如何获得所有组合以及我们拥有的数组数量?

我们循环遍历我们已经拥有的所有组合(从一个组合开始,一个“空组合”($combinations = [[]];)),对于每个组合,我们遍历下一个数据数组并将每个组合与每个组合输入数据到一个新的组合。

现在我们这样做,直到我们为每个组合获得所需的长度。

举个例子:

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]  //desired length 2 as we have 2 arrays 
                  - [1]   + 4  │  -> [1,4]  //desired length 2 as we have 2 arrays 
                  - [2]   + 3  │  -> [2,3]  //desired length 2 as we have 2 arrays 
                  - [2]   + 4  │  -> [2,4]  //desired length 2 as we have 2 arrays    
                               //↑ All combinations here

* NAN:不是数字

正如您在上面的示例中看到的那样,我们现在拥有所有组合以及我们拥有的所有数组数量的长度。

但是为了只获得具有所需长度的组合,我们每次迭代都会覆盖结果数组,这样最后只有具有预期长度的组合才会出现在结果数组中。

代码:

<?php

    $array1 = array(1,2);
    $array2 = array(4,5);
    $array3 = array(7,8);


    $combinations = [[]];
    $data = [
        $array1,
        $array2,
        $array3,
    ];
    $length = count($data);

    for ($count = 0; $count < $length; $count++) 
        $tmp = [];
        foreach ($combinations as $v1) 
            foreach ($data[$count] as $v2)
                $tmp[] = array_merge($v1, [$v2]);

        
        $combinations = $tmp;
    

    print_r($combinations);

?>

输出:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 4
            [2] => 7
        )
    //...
    [7] => Array
        (
            [0] => 2
            [1] => 5
            [2] => 8
        )

)

对于关联数组,您只需稍作修改,即:

    首先将数组键分配给带有array_keys()的变量,例如

    $keys = array_keys($data);
    

    使用第二个foreach循环中的keys来访问数据数组,表示from:

    foreach ($data[$count] as $v2)
    

    到:

    foreach ($data[$keys[$count]] as $v2)

【讨论】:

【参考方案2】:
<?php

$color = array('Blue','Red','Black','Green');
$size = array('L','M','S','XL','XXL');
$type  = array('Half selevs','full seleves');
$options = [
            $color,
            $size,
            $type,
        ];
$combinations = getCombinations($options);

print_r($combinations);

function getCombinations($options) 

            $combinations = [[]];

            for ($count = 0; $count < count($options); $count++) 
                $tmp = [];
                foreach ($combinations as $v1) 
                    foreach ($options[$count] as $v2)
                        $tmp[] = array_merge($v1, [$v2]);

                
                $combinations = $tmp;
            

            return $combinations;
        
?>

输出:

Array
(
    [0] => Array
        (
            [0] => Blue
            [1] => L
            [2] => Half selevs
        )

    [1] => Array
        (
            [0] => Blue
            [1] => L
            [2] => full seleves
        )

    [2] => Array
        (
            [0] => Blue
            [1] => M
            [2] => Half selevs
        )

    [3] => Array
        (
            [0] => Blue
            [1] => M
            [2] => full seleves
        )

    [4] => Array
        (
            [0] => Blue
            [1] => S
            [2] => Half selevs
        )

    [5] => Array
        (
            [0] => Blue
            [1] => S
            [2] => full seleves
        )

    [6] => Array
        (
            [0] => Blue
            [1] => XL
            [2] => Half selevs
        )

    [7] => Array
        (
            [0] => Blue
            [1] => XL
            [2] => full seleves
        )

    [8] => Array
        (
            [0] => Blue
            [1] => XXL
            [2] => Half selevs
        )

    [9] => Array
        (
            [0] => Blue
            [1] => XXL
            [2] => full seleves
        )

    [10] => Array
        (
            [0] => Red
            [1] => L
            [2] => Half selevs
        )

    [11] => Array
        (
            [0] => Red
            [1] => L
            [2] => full seleves
        )

    [12] => Array
        (
            [0] => Red
            [1] => M
            [2] => Half selevs
        )

    [13] => Array
        (
            [0] => Red
            [1] => M
            [2] => full seleves
        )

    [14] => Array
        (
            [0] => Red
            [1] => S
            [2] => Half selevs
        )

    [15] => Array
        (
            [0] => Red
            [1] => S
            [2] => full seleves
        )

    [16] => Array
        (
            [0] => Red
            [1] => XL
            [2] => Half selevs
        )

    [17] => Array
        (
            [0] => Red
            [1] => XL
            [2] => full seleves
        )

    [18] => Array
        (
            [0] => Red
            [1] => XXL
            [2] => Half selevs
        )

    [19] => Array
        (
            [0] => Red
            [1] => XXL
            [2] => full seleves
        )

    [20] => Array
        (
            [0] => Black
            [1] => L
            [2] => Half selevs
        )

    [21] => Array
        (
            [0] => Black
            [1] => L
            [2] => full seleves
        )

    [22] => Array
        (
            [0] => Black
            [1] => M
            [2] => Half selevs
        )

    [23] => Array
        (
            [0] => Black
            [1] => M
            [2] => full seleves
        )

    [24] => Array
        (
            [0] => Black
            [1] => S
            [2] => Half selevs
        )

    [25] => Array
        (
            [0] => Black
            [1] => S
            [2] => full seleves
        )

    [26] => Array
        (
            [0] => Black
            [1] => XL
            [2] => Half selevs
        )

    [27] => Array
        (
            [0] => Black
            [1] => XL
            [2] => full seleves
        )

    [28] => Array
        (
            [0] => Black
            [1] => XXL
            [2] => Half selevs
        )

    [29] => Array
        (
            [0] => Black
            [1] => XXL
            [2] => full seleves
        )

    [30] => Array
        (
            [0] => Green
            [1] => L
            [2] => Half selevs
        )

    [31] => Array
        (
            [0] => Green
            [1] => L
            [2] => full seleves
        )

    [32] => Array
        (
            [0] => Green
            [1] => M
            [2] => Half selevs
        )

    [33] => Array
        (
            [0] => Green
            [1] => M
            [2] => full seleves
        )

    [34] => Array
        (
            [0] => Green
            [1] => S
            [2] => Half selevs
        )

    [35] => Array
        (
            [0] => Green
            [1] => S
            [2] => full seleves
        )

    [36] => Array
        (
            [0] => Green
            [1] => XL
            [2] => Half selevs
        )

    [37] => Array
        (
            [0] => Green
            [1] => XL
            [2] => full seleves
        )

    [38] => Array
        (
            [0] => Green
            [1] => XXL
            [2] => Half selevs
        )

    [39] => Array
        (
            [0] => Green
            [1] => XXL
            [2] => full seleves
        )

)

【讨论】:

以上是关于如何从多个数组中获取所有组合?的主要内容,如果未能解决你的问题,请参考以下文章

从多个嵌套数组中获取所有组合

如何在 PHP 中生成多个数组中的所有项目组合

从数组(Java)中获取所有大小 n 组合的算法? [关闭]

输入n个数组,数组长度不等,每个数组取出一个数进行组合,求出所有的组合。

从多个值列表中查找所有不冲突的值组合

mysql - 从多个表中获取相关行而不使用所有组合