Group discussions on multiple arrays
Let's say there are three arrays (actually indeterminate number of groups):
$arr 1 = Array (1, 2, 3, 4);
$arr 2 = Array (10, 20, 30, 40);
$arr 3 = Array (100, 200, 300, 400);
To achieve the result:
Array
Array (1,10,100),
Array (1,10,200),
Array (1,10,300),
Array (1,10,400),
Array (1,20,100),
Array (1,20,200),
Array (1,20,300),
Array (1,20,400),
Array (1,30,100),
...
);
Do not want to use recursion, but can also be implemented to see
permutation combinations
Share to:
------Solution--------------------
Can the array () be simplified to [] is php5.4? How do I remember php5.3?
Simply comment:
$d = Func_get_args ();//reads the passed-in parameter into the array (PHP4)
if (count ($d) < 2) return current ($d);
$n = array_product (Array_map (' count ', $d)); Array_product computes the product of the array value (PHP5), ARRAY_MAP returns the array (PHP4) processed by the callback function
Array_walk ($d, function (& $v, $k, $n) {//Use callback function to handle array element (PHP3) closure notation (php5.3)
$v = Array_chunk (Call_user_func_array (' Array_merge ', Array_pad ([$v], $n/count ($v), $v)), 1);
}, $n);//array_chunk cut Array (PHP4)
Call_user_func_array calling a specified function and passing a parameter array (PHP4) is useful for functions that perform a variable number of arguments
foreach ($d as $i = = $t) {
if ($i = = 0) {
$r = $t;
}else {
Array_multisort (Array_map (function ($v) {return $v [0];}, $r), $r);
$r = Array_map (' Array_merge ', $r, $t);
}
}
------Solution--------------------
function my_arr_combine ()
{
$args = Func_get_args ();
$final = Array_shift ($args);
while ($f = Array_shift ($args))
{
$r = array ();