如何从多个数组中获取所有组合? [英] How to get all combinations from multiple arrays?

查看:23
本文介绍了如何从多个数组中获取所有组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这 3 个数组

Supposing I have these 3 arrays

$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 只是为了不为空)或有很多值.如果我有一个空数组,我还需要将其计为有效列.这些值将用于以特定顺序填充数据库.

One of my problems is that my array myght vary from 3 to 15 different arrays and each myght be empty (I might add a 0 just not to be empty) or have many values. If I have an empty array I also need to count that as a valid column. These values will be used to fill up a database in a specific order.

有什么办法可以做到这一点吗?

Is there any way I can do this?

推荐答案

有多少种组合?

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

How many combinations are there?

So first the question how many combinations are there? And the answer is you have to multiply the amount of every array with each other.

所以(c = 数量1):

So (c = amount1):

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

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

具体针对您的示例:

carray 1 * carray 2 * carray 3 = 2 * 2 * 2 = 8

carray 1 * carray 2 * carray 3 = 2 * 2 * 2 = 8

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

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

How do we get now all combinations with the amount of arrays, which we have?

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

We loop through all our combinations, which we already have(Starting off with one combination, an "empty combination" ($combinations = [[]];)), and for each combination we go through our next data array and combine each combination with each input data to a new combination.

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

Now we do this until we get the desired length for each combination.

举个例子:

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:不是数字

正如你在上面的例子中看到的,我们现在拥有了所有数组长度的所有组合.

So as you can see in the above example we now have all combinations with the length of the amount of all arrays which we have.

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

But to get only the combinations with the desired length we are overwriting the result array each iteration, so that at the end only the combinations with the expected length are in the results array.

<?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
        )

)


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


For associative arrays you only have to do a slight modification, which is:

  1. 首先使用 array_keys() 将数组键分配给一个变量,例如

  1. First assign the arrays keys to a variable with array_keys(), e.g.

$keys = array_keys($data);

  • 使用第二个foreach循环中的key来访问数据数组,意思是:

  • Use the keys in the second foreach loop to access the data array, means from:

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

    到:

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

  • 这篇关于如何从多个数组中获取所有组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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