简体   繁体   English

获取多维数组的所有变体

[英]Get all variations of multidimensional array

I have an array, for example: 我有一个数组,例如:

Array
(
    [2] => Array
        (
            [3] => Yes
            [4] => No
        )
    [4] => Array
        (
            [28] => Maroon
            [29] => Red
            [30] => Dark Blue
            [31] => Grey
            [32] => Pastel Blue
            [33] => Yellow
            [34] => Green
        )    
    [7] => Array
        (
            [265] => Var 1
            [266] => Var 2
            [267] => Var 3
            [268] => Var 4
        )    
    [87] => Array
        (
            [1888] => 1800h x 450w x 450d (1845N)
        )    
)

I would like to get all unique variation of the arrays for products options, eg 我想获得所有阵列的产品选项的唯一变体,例如

Array
(
    [2] => 3
    [4] => 28
    [7] => 265
    [87] => 1888
)
Array
(
    [2] => 3
    [4] => 29
    [7] => 265
    [87] => 1888
)

and so forth. 等等。 I've added all the details i need to add, however apparently I need to add more details. 我已经添加了所有需要添加的细节,但是显然我需要添加更多细节。

<?php
function comb($a){
    $out = array();
    if (count($a) == 1) {
        $x = array_shift($a);
        foreach ($x as $v) $out[] = array($v);
        return $out;
    }
    foreach ($a as $k => $v){
        $b = $a;
        unset($b[$k]);
        $x = comb($b);
        foreach ($v as $v1){
            foreach ($x As $v2) 
            $out[] = array_merge(array($v1), $v2);
        }
    }
    return $out;
}


$test = array(array('red', 'blue', 'green'),array('small', 'med', 'large'),array('car', 'truck', 'van'));

$x = comb($test);
print_r($x);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM