php排列组合算法

<?php
    $arr=[['a','b','c','d'],['2','3','4','5'],[',','.','?',';']];
    $num = count($arr);
    if ($num === 0) return false;
    if ($num === 1) return $arr[0];
    while(count($arr) > 1) {
        $arr_first = array_shift($arr);
        $arr_second = array_shift($arr);
        $c = array();
        foreach ($arr_first as $v) {
            $v = (array) $v;
            foreach ($arr_second as $val) {
                $c[] = array_merge($v, (array) $val);
            }
        }
        array_unshift($arr, $c);
        unset($c);
    }
    print_r($arr);exit;
    return $arr[0];
?>

结果:

 

 

posted @ 2019-12-23 09:27  zrn  阅读(757)  评论(0编辑  收藏  举报