php数组 组合排列 笛卡尔积
程序员文章站
2022-10-06 13:05:32
function Descartes() { $t = func_get_args(); if (func_num_args() == 1) { return call_user_func_array(__FUNCTION__, $t[0]); } $a = array_shift($t); if ... ......
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);