这应该适用于任意数量的数组:
<?php
$arr = array(
array(1, 2, 3),
array(10, 20, 30),
array(100, 200, 300),
);
print_r(foo(array(), $arr));
function foo($arr1 = array(), $arr2 = array()) {
$perm_arr = array();
// case 1: empty array vs outer array
// eg: array() vs array(array(1, 2, 3), array(10, 20, 30), array(100, 200, 300))
// recursively reduce the size of outer array via case 2
if(empty($arr1) && (isset($arr2[0]) && is_array($arr2[0]))) {
$arr_top = array();
$arr_bot = $arr2;
// get possible arrangements for further reduction
// eg:
// array(1, 2, 3) vs array(array(10, 20, 30), array(100, 200, 300)),
// array(10, 20, 30) vs array(array(1, 2, 3), array(100, 200, 300)),
// array(100, 200, 300) vs array(array(1, 2, 3), array(10, 20, 30)),
// ...
while(0 < count($arr_bot)) {
$arr = array_shift($arr_bot);
$arr_edge = array_merge($arr_top, $arr_bot);
$foo_arr_edge = foo($arr, $arr_edge);
// append permutations of reduced array
foreach($foo_arr_edge as $e_edge) {
$perm_arr[] = $e_edge;
}
$arr_top[] = $arr;
}
}
// case 2: inner array vs outer array
// eg: array(1, 2, 3) vs array(array(10, 20, 30), array(100, 200, 300))
// reduce the size of outer array until it reaches the base case
else if(isset($arr1[0]) && (isset($arr2[0]) && is_array($arr2[0]))) {
$foo_arr2 = array();
$n_arr2 = count($arr2);
// if the size of outer array is greater than 2 then reduce it until the size is 2
if(2 < $n_arr2) {
$arr_top = array();
$arr_bot = $arr2;
// get possible arrangements for further reduction
// eg:
// array(1, 2, 3) vs array(10, 20, 30),
// array(1, 2, 3) vs array(100, 200, 300),
// array(10, 20, 30) vs array(100, 200, 300),
// ...
while(0 < count($arr_bot)) {
$arr = array_shift($arr_bot);
$arr_edge = array_merge($arr_top, $arr_bot);
$foo_arr_edge = foo($arr, $arr_edge);
foreach($foo_arr_edge as $e_edge) {
$foo_arr2[] = $e_edge;
}
$arr_top[] = $arr;
}
}
// if the size of outer array is 2 then get the permutations of its elements via the base case
else if(2 == $n_arr2) {
$foo_arr2 = foo($arr2[0], $arr2[1]);
}
// generate permutations from reduced array
foreach($arr1 as $e1) {
foreach($foo_arr2 as $e2) {
$perm = $e1 .'-'. $e2;
$perm_arr[] = $perm;
}
}
}
// base case: inner array vs inner array
// eg: array(1, 2, 3) vs array(10, 20, 30)
// generate permutations of two inner arrays
else if(isset($arr1[0]) && (isset($arr2[0]) && !is_array($arr2[0]))) {
foreach($arr1 as $e1) {
foreach($arr2 as $e2) {
$perm = $e1 .'-'. $e2;
$perm_arr[] = $perm;
}
}
foreach($arr2 as $e2) {
foreach($arr1 as $e1) {
$perm = $e2 .'-'. $e1;
$perm_arr[] = $perm;
}
}
}
return $perm_arr;
}
?>