How to generate in PHP all combinations of items in multiple arrays

前端 未结 8 775
野性不改
野性不改 2020-11-22 07:20

I\'im trying to find all combinations of items in several arrays. The number of arrays is random (this can be 2, 3, 4, 5...). The number of elements in each array is random

8条回答
  •  渐次进展
    2020-11-22 07:54

    I like this solution: https://stackoverflow.com/a/33259643/3163536 but to answer the actual question (which assumes that the number of elements of each combination should be equal to the number of incoming arrays) the function should be modified:

    function getCombinations(...$arrays)
        {
            $result = [[]];
            foreach ($arrays as $property => $property_values) {
                $tmp = [];
                foreach ($result as $result_item) {
                    foreach ($property_values as $property_value) {
                        $tmp[] = array_merge($result_item, [$property => $property_value]);
                    }
                }
                $result = $tmp;
            }
            return $result;
        }
    

    The usage:

    $arrayA = array('A1','A2','A3');
    $arrayB = array('B1','B2','B3');
    $arrayC = array('C1','C2');
    
    print_r(getCombinations($arrayA, $arrayB, $arrayC));
    

    The result:

    Array
    (
        [0] => Array
            (
                [0] => A1
                [1] => B1
                [2] => C1
            )
    
        [1] => Array
            (
                [0] => A1
                [1] => B1
                [2] => C2
            )
    
        [2] => Array
            (
                [0] => A1
                [1] => B2
                [2] => C1
            )
    
        [3] => Array
            (
                [0] => A1
                [1] => B2
                [2] => C2
            )
    
        [4] => Array
            (
                [0] => A1
                [1] => B3
                [2] => C1
            )
    
        [5] => Array
            (
                [0] => A1
                [1] => B3
                [2] => C2
            )
    
        [6] => Array
            (
                [0] => A2
                [1] => B1
                [2] => C1
            )
    
        [7] => Array
            (
                [0] => A2
                [1] => B1
                [2] => C2
            )
    
        [8] => Array
            (
                [0] => A2
                [1] => B2
                [2] => C1
            )
    
        [9] => Array
            (
                [0] => A2
                [1] => B2
                [2] => C2
            )
    
        [10] => Array
            (
                [0] => A2
                [1] => B3
                [2] => C1
            )
    
        [11] => Array
            (
                [0] => A2
                [1] => B3
                [2] => C2
            )
    
        [12] => Array
            (
                [0] => A3
                [1] => B1
                [2] => C1
            )
    
        [13] => Array
            (
                [0] => A3
                [1] => B1
                [2] => C2
            )
    
        [14] => Array
            (
                [0] => A3
                [1] => B2
                [2] => C1
            )
    
        [15] => Array
            (
                [0] => A3
                [1] => B2
                [2] => C2
            )
    
        [16] => Array
            (
                [0] => A3
                [1] => B3
                [2] => C1
            )
    
        [17] => Array
            (
                [0] => A3
                [1] => B3
                [2] => C2
            )
    
    )
    

提交回复
热议问题