php数组 组合排列 笛卡尔积

function Descartes()
{
    $t = func_get_args();
    if (func_num_args() == 1) {
        return call_user_func_array(__FUNCTION__, $t[0]);
    }

    $a = array_shift($t);
    if (!is_array($a)) {
        $a = array($a);
    }

    $a = array_chunk($a, 1);
    do {
        $r = array();
        $b = array_shift($t);
        if (!is_array($b)) {
            $b = array($b);
        }

        foreach ($a as $p) {
            foreach (array_chunk($b, 1) as $q) {
                $r[] = array_merge($p, $q);
            }
        }

        $a = $r;
    } while ($t);
    return $r;
}

$arr = [
    ['3101','3102','3103'],
    ['3201','3202'],
];

$tmp = Descartes($arr);

print_r($tmp);

 

posted @ 2019-03-27 13:15  没有昵称不想想  阅读(273)  评论(0编辑  收藏