How to get all combinations from multiple arrays?

最后都变了- 提交于 2019-12-28 03:10:50

问题


Supposing I have these 3 arrays

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

I need this output

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

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?


回答1:


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.

So (c = amount1):

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

And specific for your example:

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

*1 And if you wonder why I chose c for amount, because of the function count() in php

Getting all combinations together

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

* NAN: not a number

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.

Code:

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

?>

output:

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. First assign the arrays keys to a variable with array_keys(), e.g.

    $keys = array_keys($data);
    
  2. Use the keys in the second foreach loop to access the data array, means from:

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

    to:

    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;
        }
?>

Output:

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
        )

)


来源:https://stackoverflow.com/questions/30512570/how-to-get-all-combinations-from-multiple-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!