Combinations of all sub array elements without repeats

前端 未结 2 613
悲&欢浪女
悲&欢浪女 2021-01-16 03:14

I have \"attributes\" from database. Each attribute has many values. Now I want to mix these values to create unique combinations.

example of input:

         


        
2条回答
  •  粉色の甜心
    2021-01-16 03:32

    This should work for you:

    So what does this code do?

    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 = 3 * 3 = 9

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

    2. Getting all combinations

    How do we get now all combinations with the length of the amount of all arrays?

    Well pretty simple, we just loop through all combinations (at the start just an empty combination ([] == array())) which we already have with the next array until we get the desired length which we want, in this case the last iteration of the last array.

    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]
                      - [1]   + 4  |  -> [1,4]
                      - [2]   + 3  |  -> [2,3]
                      - [2]   + 4  |  -> [2,4]     
                                   //^ 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:

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

    output:

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

提交回复
热议问题