从阵列生成所有可能的变体

时间:2016-11-24 10:44:29

标签: php arrays

我有一个像https://3v4l.org/VrIDe

这样的数组
$arr = array(
'slidelink' => 'presentation.pptx',
'productid' => array(1,3),
'order' => 2,
'class_id' => array(1,2),
'currency_id' => array(1,2),
'presentation_type' => 1,
'distribution' => 0,

);

我希望将它的所有可能输出生成到一个新数组,所以在这种情况下有8行像:

$new_arr = array (
 0 => array(presentation.pptx, 1, 2, 1, 1, 1, 0),
 1 => array(presentation.pptx, 1, 2, 1, 2, 1, 0),
 2 => array(presentation.pptx, 1, 2, 2, 1, 1, 0),
 3 => array(presentation.pptx, 1, 2, 2, 2, 1, 0),
 4 => array(presentation.pptx, 3, 2, 1, 1, 1, 0),
 5 => array(presentation.pptx, 3, 2, 1, 2, 1, 0),
 6 => array(presentation.pptx, 3, 2, 2, 1, 1, 0),
 7 => array(presentation.pptx, 3, 2, 2, 2, 1, 0)
);

我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:2)

实际上,您的问题是更具体的情况(未存储数组键),但这是通用的方法:

$arr = [
    'size'  => [ 'XS', 'S', 'M', 'L', 'XL', 'XXL' ],
    'color' => [ 'yellow', 'brown', 'white' ],
    'weight'=> [ 'super', 'normal' => [ 'extra', 'high' => [ 'super', 'low' => 'lower' ] ], 'extra' , 'medium', 'heavy', 'overload' ]
];


function variations( $array ){

    if( empty( $array ) ) return [];

    function traverse( $array, $parent_ind ){
        $r = [];
        $pr = '';
        if( !is_numeric($parent_ind) )
            $pr = $parent_ind . '-';
        foreach( $array as $ind=>$el ) {
            if ( is_array( $el ) ) {
                $r = array_merge( $r, traverse( $el, $pr . ( is_numeric( $ind ) ? '' : $ind ) ) );
            }else
                if ( is_numeric( $ind ) )
                    $r[] = $pr . $el;
                else
                    $r[] = $pr . $ind . '-' . $el;
        }
        return $r;
    }

    //1. Go through entire array and transform elements that are arrays into elements, collect keys
    $keys = [];$size = 1;
    foreach( $array as $key=>$elems ) {
        if ( is_array( $elems ) ) {
            $rr = [];
            foreach ( $elems as $ind=>$elem ) {
                if ( is_array( $elem ) )
                    $rr = array_merge( $rr, traverse( $elem, $ind ) );
                else $rr[] = $elem;
            }
            $array[ $key ] = $rr;
            $size *= count( $rr );
        }
        $keys[] = $key;
    }

    //2. Go through all new elems and make variations
    $rez = [];
    for( $i = 0; $i < $size; $i++ ) {
        $rez[ $i ] = array();
        foreach( $array as $key => $value ){
            $current = current( $array[ $key ] );
            $rez[ $i ][ $key ] = $current;
        }
        foreach( $keys as $key )
            if( !next( $array[ $key ] ) ) reset( $array[ $key ] );
            else break;
    }

    return $rez;

}


die( print_r( variations( $arr ) ) );

结果将

Array
(
    [0] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => super
        )

    [1] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => super
        )

    [2] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => super
        )

    [3] => Array
        (
            [size] => L
            [color] => yellow
            [weight] => super
        )

    [4] => Array
        (
            [size] => XL
            [color] => yellow
            [weight] => super
        )

    [5] => Array
        (
            [size] => XXL
            [color] => yellow
            [weight] => super
        )

    [6] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => super
        )

    [7] => Array
        (
            [size] => S
            [color] => brown
            [weight] => super
        )

    [8] => Array
        (
            [size] => M
            [color] => brown
            [weight] => super
        )

    [9] => Array
        (
            [size] => L
            [color] => brown
            [weight] => super
        )

    [10] => Array
        (
            [size] => XL
            [color] => brown
            [weight] => super
        )

    [11] => Array
        (
            [size] => XXL
            [color] => brown
            [weight] => super
        )

    [12] => Array
        (
            [size] => XS
            [color] => white
            [weight] => super
        )

    [13] => Array
        (
            [size] => S
            [color] => white
            [weight] => super
        )

    [14] => Array
        (
            [size] => M
            [color] => white
            [weight] => super
        )

    [15] => Array
        (
            [size] => L
            [color] => white
            [weight] => super
        )

    [16] => Array
        (
            [size] => XL
            [color] => white
            [weight] => super
        )

    [17] => Array
        (
            [size] => XXL
            [color] => white
            [weight] => super
        )

    [18] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => normal-extra
        )

    [19] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => normal-extra
        )

    [20] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => normal-extra
        )

    [21] => Array
        (
            [size] => L
            [color] => yellow
            [weight] => normal-extra
        )

    [22] => Array
        (
            [size] => XL
            [color] => yellow
            [weight] => normal-extra
        )

    [23] => Array
        (
            [size] => XXL
            [color] => yellow
            [weight] => normal-extra
        )

    [24] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => normal-extra
        )

    [25] => Array
        (
            [size] => S
            [color] => brown
            [weight] => normal-extra
        )

    [26] => Array
        (
            [size] => M
            [color] => brown
            [weight] => normal-extra
        )

    [27] => Array
        (
            [size] => L
            [color] => brown
            [weight] => normal-extra
        )

    [28] => Array
        (
            [size] => XL
            [color] => brown
            [weight] => normal-extra
        )

    [29] => Array
        (
            [size] => XXL
            [color] => brown
            [weight] => normal-extra
        )

    [30] => Array
        (
            [size] => XS
            [color] => white
            [weight] => normal-extra
        )

    [31] => Array
        (
            [size] => S
            [color] => white
            [weight] => normal-extra
        )

    [32] => Array
        (
            [size] => M
            [color] => white
            [weight] => normal-extra
        )

    [33] => Array
        (
            [size] => L
            [color] => white
            [weight] => normal-extra
        )

    [34] => Array
        (
            [size] => XL
            [color] => white
            [weight] => normal-extra
        )

    [35] => Array
        (
            [size] => XXL
            [color] => white
            [weight] => normal-extra
        )

    [36] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => normal-high-super
        )

    [37] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => normal-high-super
        )

    [38] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => normal-high-super
        )

    [39] => Array
        (
            [size] => L
            [color] => yellow
            [weight] => normal-high-super
        )

    [40] => Array
        (
            [size] => XL
            [color] => yellow
            [weight] => normal-high-super
        )

    [41] => Array
        (
            [size] => XXL
            [color] => yellow
            [weight] => normal-high-super
        )

    [42] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => normal-high-super
        )

    [43] => Array
        (
            [size] => S
            [color] => brown
            [weight] => normal-high-super
        )

    [44] => Array
        (
            [size] => M
            [color] => brown
            [weight] => normal-high-super
        )

    [45] => Array
        (
            [size] => L
            [color] => brown
            [weight] => normal-high-super
        )

    [46] => Array
        (
            [size] => XL
            [color] => brown
            [weight] => normal-high-super
        )

    [47] => Array
        (
            [size] => XXL
            [color] => brown
            [weight] => normal-high-super
        )

    [48] => Array
        (
            [size] => XS
            [color] => white
            [weight] => normal-high-super
        )

    [49] => Array
        (
            [size] => S
            [color] => white
            [weight] => normal-high-super
        )

    [50] => Array
        (
            [size] => M
            [color] => white
            [weight] => normal-high-super
        )

    [51] => Array
        (
            [size] => L
            [color] => white
            [weight] => normal-high-super
        )

    [52] => Array
        (
            [size] => XL
            [color] => white
            [weight] => normal-high-super
        )

    [53] => Array
        (
            [size] => XXL
            [color] => white
            [weight] => normal-high-super
        )

    [54] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => normal-high-low-lower
        )

    [55] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => normal-high-low-lower
        )

    [56] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => normal-high-low-lower
        )

    [57] => Array
        (
            [size] => L
            [color] => yellow
            [weight] => normal-high-low-lower
        )

    [58] => Array
        (
            [size] => XL
            [color] => yellow
            [weight] => normal-high-low-lower
        )

    [59] => Array
        (
            [size] => XXL
            [color] => yellow
            [weight] => normal-high-low-lower
        )

    [60] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => normal-high-low-lower
        )

    [61] => Array
        (
            [size] => S
            [color] => brown
            [weight] => normal-high-low-lower
        )

    [62] => Array
        (
            [size] => M
            [color] => brown
            [weight] => normal-high-low-lower
        )

    [63] => Array
        (
            [size] => L
            [color] => brown
            [weight] => normal-high-low-lower
        )

    [64] => Array
        (
            [size] => XL
            [color] => brown
            [weight] => normal-high-low-lower
        )

    [65] => Array
        (
            [size] => XXL
            [color] => brown
            [weight] => normal-high-low-lower
        )

    [66] => Array
        (
            [size] => XS
            [color] => white
            [weight] => normal-high-low-lower
        )

    [67] => Array
        (
            [size] => S
            [color] => white
            [weight] => normal-high-low-lower
        )

    [68] => Array
        (
            [size] => M
            [color] => white
            [weight] => normal-high-low-lower
        )

    [69] => Array
        (
            [size] => L
            [color] => white
            [weight] => normal-high-low-lower
        )

    [70] => Array
        (
            [size] => XL
            [color] => white
            [weight] => normal-high-low-lower
        )

    [71] => Array
        (
            [size] => XXL
            [color] => white
            [weight] => normal-high-low-lower
        )

    [72] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => extra
        )

    [73] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => extra
        )

    [74] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => extra
        )

    [75] => Array
        (
            [size] => L
            [color] => yellow
            [weight] => extra
        )

    [76] => Array
        (
            [size] => XL
            [color] => yellow
            [weight] => extra
        )

    [77] => Array
        (
            [size] => XXL
            [color] => yellow
            [weight] => extra
        )

    [78] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => extra
        )

    [79] => Array
        (
            [size] => S
            [color] => brown
            [weight] => extra
        )

    [80] => Array
        (
            [size] => M
            [color] => brown
            [weight] => extra
        )

    [81] => Array
        (
            [size] => L
            [color] => brown
            [weight] => extra
        )

    [82] => Array
        (
            [size] => XL
            [color] => brown
            [weight] => extra
        )

    [83] => Array
        (
            [size] => XXL
            [color] => brown
            [weight] => extra
        )

    [84] => Array
        (
            [size] => XS
            [color] => white
            [weight] => extra
        )

    [85] => Array
        (
            [size] => S
            [color] => white
            [weight] => extra
        )

    [86] => Array
        (
            [size] => M
            [color] => white
            [weight] => extra
        )

    [87] => Array
        (
            [size] => L
            [color] => white
            [weight] => extra
        )

    [88] => Array
        (
            [size] => XL
            [color] => white
            [weight] => extra
        )

    [89] => Array
        (
            [size] => XXL
            [color] => white
            [weight] => extra
        )

    [90] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => medium
        )

    [91] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => medium
        )

    [92] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => medium
        )

    [93] => Array
        (
            [size] => L
            [color] => yellow
            [weight] => medium
        )

    [94] => Array
        (
            [size] => XL
            [color] => yellow
            [weight] => medium
        )

    [95] => Array
        (
            [size] => XXL
            [color] => yellow
            [weight] => medium
        )

    [96] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => medium
        )

    [97] => Array
        (
            [size] => S
            [color] => brown
            [weight] => medium
        )

    [98] => Array
        (
            [size] => M
            [color] => brown
            [weight] => medium
        )

    [99] => Array
        (
            [size] => L
            [color] => brown
            [weight] => medium
        )

    [100] => Array
        (
            [size] => XL
            [color] => brown
            [weight] => medium
        )

    [101] => Array
        (
            [size] => XXL
            [color] => brown
            [weight] => medium
        )

    [102] => Array
        (
            [size] => XS
            [color] => white
            [weight] => medium
        )

    [103] => Array
        (
            [size] => S
            [color] => white
            [weight] => medium
        )

    [104] => Array
        (
            [size] => M
            [color] => white
            [weight] => medium
        )

    [105] => Array
        (
            [size] => L
            [color] => white
            [weight] => medium
        )

    [106] => Array
        (
            [size] => XL
            [color] => white
            [weight] => medium
        )

    [107] => Array
        (
            [size] => XXL
            [color] => white
            [weight] => medium
        )

    [108] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => heavy
        )

    [109] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => heavy
        )

    [110] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => heavy
        )

    [111] => Array
        (
            [size] => L
            [color] => yellow
            [weight] => heavy
        )

    [112] => Array
        (
            [size] => XL
            [color] => yellow
            [weight] => heavy
        )

    [113] => Array
        (
            [size] => XXL
            [color] => yellow
            [weight] => heavy
        )

    [114] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => heavy
        )

    [115] => Array
        (
            [size] => S
            [color] => brown
            [weight] => heavy
        )

    [116] => Array
        (
            [size] => M
            [color] => brown
            [weight] => heavy
        )

    [117] => Array
        (
            [size] => L
            [color] => brown
            [weight] => heavy
        )

    [118] => Array
        (
            [size] => XL
            [color] => brown
            [weight] => heavy
        )

    [119] => Array
        (
            [size] => XXL
            [color] => brown
            [weight] => heavy
        )

    [120] => Array
        (
            [size] => XS
            [color] => white
            [weight] => heavy
        )

    [121] => Array
        (
            [size] => S
            [color] => white
            [weight] => heavy
        )

    [122] => Array
        (
            [size] => M
            [color] => white
            [weight] => heavy
        )

    [123] => Array
        (
            [size] => L
            [color] => white
            [weight] => heavy
        )

    [124] => Array
        (
            [size] => XL
            [color] => white
            [weight] => heavy
        )

    [125] => Array
        (
            [size] => XXL
            [color] => white
            [weight] => heavy
        )

    [126] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => overload
        )

    [127] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => overload
        )

    [128] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => overload
        )

    [129] => Array
        (
            [size] => L
            [color] => yellow
            [weight] => overload
        )

    [130] => Array
        (
            [size] => XL
            [color] => yellow
            [weight] => overload
        )

    [131] => Array
        (
            [size] => XXL
            [color] => yellow
            [weight] => overload
        )

    [132] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => overload
        )

    [133] => Array
        (
            [size] => S
            [color] => brown
            [weight] => overload
        )

    [134] => Array
        (
            [size] => M
            [color] => brown
            [weight] => overload
        )

    [135] => Array
        (
            [size] => L
            [color] => brown
            [weight] => overload
        )

    [136] => Array
        (
            [size] => XL
            [color] => brown
            [weight] => overload
        )

    [137] => Array
        (
            [size] => XXL
            [color] => brown
            [weight] => overload
        )

    [138] => Array
        (
            [size] => XS
            [color] => white
            [weight] => overload
        )

    [139] => Array
        (
            [size] => S
            [color] => white
            [weight] => overload
        )

    [140] => Array
        (
            [size] => M
            [color] => white
            [weight] => overload
        )

    [141] => Array
        (
            [size] => L
            [color] => white
            [weight] => overload
        )

    [142] => Array
        (
            [size] => XL
            [color] => white
            [weight] => overload
        )

    [143] => Array
        (
            [size] => XXL
            [color] => white
            [weight] => overload
        )

)

答案 1 :(得分:1)

对所有子数组的数组元素使用嵌套循环。

要处理元素可能不是数组的可能性,可以使用强制转换将其转换为数组。

$new_arr = array();
foreach ((array)$arr['slidelink'] as $s) {
    foreach ((array)$arr['productid'] as $pid) {
        foreach ((array)$arr['order'] as $o) {
            foreach ((array)$arr['class_id'] as $cid) {
                foreach ((array)$arr['currency_id'] as $curr) {
                    foreach ((array)$arr['presentation_type'] as $p) {
                        foreach ((array)$arr['distribution'] as $d) {
                            $new_arr[] = array($s, $pid, $o, $cid, $curr, $p, $d);
                        }
                    }
                }
            }
        }
    }
}