数组遍历组合

基于不定长数组,进行排列组合


$target = [

    [11,12,13,14,15],

    [21,22,23,24,25],

    [31,32,33,34,35],

    [41,42,43,44,45],

    [51,52,53,54,55],

];


function comb($input, &$output = [], $prefix = "") {

    if (count($input) > 1) {

        $cur_arr = array_shift($input);;

        foreach($cur_arr as $index => $_num) {

            comb($input, $output, $prefix . "," . $_num);

        }   

    }else{

        foreach($input[0] as $num) {

            echo substr($prefix . "," . $num . "\n", 1)

            $output[] = explode(",", substr($prefix . "," . $num, 1));

        }   

    }   

}

comb($target, $output);

//var_dump($output);

你可能感兴趣的:(代码)