如何从多个阵列的所有组合? [英] How to get all combinations from multiple arrays?

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

问题描述

假如我有这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

我的一个问题是,我的数组myght从3到15个不同的磁盘阵列的每个myght是空的(我想补充一个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

C <子>阵列1 * C 阵列2 * ...... * C 阵列ñ

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

和特定于您的例子:

C <子>阵列1 * C 阵列2 * C 阵列3 = 2 * 2 * 2 = 8

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

<子> * 1如果你想知道为什么我在PHP选择c代表数量,因为函数计数()

我们如何得到目前所有阵列的数量,这对我们的组合?

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

我们通过我们的所有组合,这是我们已经有了循环(一个组合,一个空结合出发( $组合= [[]]; ))以及我们通过我们的下一个数据阵列的每个组合,每个输入数据结合到一个新的组合。每个组合

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.

因此​​,作为一个例子:

So as an example:

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

<子> *楠:不是一个数字

所以你可以在上面的例子中看到,我们现在所有的组合与我们有所有阵列量的长度。

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(),例如

$keys = array_keys($data);


  • 使用键在第二foreach循环来访问数据数组,从意思是:

  • 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天全站免登陆