/*方法一*/ 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 = [ ['aaa','bbb','ccc'], ['111','222'], ]; $tmp = descartes($arr); print_r($tmp);
/*方法二*/ function descartes($arr1, $arr2) { $a = array_shift($arr1); if (!is_array($a)) { $a = array($a); } $a = array_chunk($a, 1); do { $r = array(); $b = array_shift($arr2); 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; } $arr1 = [ ['aaa','bbb','ccc'], ]; $arr2 = [ ['111','222'], ]; $tmp = descartes($arr1, $arr2); print_r($tmp);