当前位置:Gxlcms > PHP教程 > 一个关于算法的,请大伙儿帮帮忙

一个关于算法的,请大伙儿帮帮忙

时间:2021-07-01 10:21:17 帮助过:5人阅读

一个关于算法的,请大家帮帮忙
$arr = array(
array( "A", "B", "C"),
array("1", "2", "3", "4"),
array("X", "Y", "Z"),
....................................
);
这样的一个数字,如何打印成下面的样子 (数组的大小不定)

A 1 X
A 1 Y
A 1 Z
A 2 X
A 2 Y
A 2 Z
A 3 X
A 3 Y
A 3 Z
A 4 X
A 4 Y
A 4 Z

B 1 X
B 1 Y
B 1 Z
B 2 X
B 2 Y
B 2 Z
B 3 X
B 3 Y
B 3 Z
B 4 X
B 4 Y
B 4 Z

C 1 X
C 1 Y
C 1 Z
C 2 X
C 2 Y
C 2 Z
C 3 X
C 3 Y
C 3 Z
C 4 X
C 4 Y
C 4 Z
------解决方案--------------------
这个符合你的输出要求
$arr = array(
array( "A", "B", "C"),
array("1", "2", "3", "4"),
array("X", "Y", "Z"),
);

function foo($d) {
$r = array_pop($d);
while($d) {
$t = array();
foreach(array_pop($d) as $k1=>$x) {
foreach($r as $k2=>$y) $t[] = "$x $y";
}
$r = $t;
}
return $r;
}
print_r(foo($arr));
Array
(
[0] => A 1 X
[1] => A 1 Y
[2] => A 1 Z
[3] => A 2 X
[4] => A 2 Y
[5] => A 2 Z
[6] => A 3 X
[7] => A 3 Y
[8] => A 3 Z
[9] => A 4 X
[10] => A 4 Y
[11] => A 4 Z
[12] => B 1 X
[13] => B 1 Y
[14] => B 1 Z
[15] => B 2 X
[16] => B 2 Y
[17] => B 2 Z
[18] => B 3 X
[19] => B 3 Y
[20] => B 3 Z
[21] => B 4 X
[22] => B 4 Y
[23] => B 4 Z
[24] => C 1 X
[25] => C 1 Y
[26] => C 1 Z
[27] => C 2 X
[28] => C 2 Y
[29] => C 2 Z
[30] => C 3 X
[31] => C 3 Y
[32] => C 3 Z
[33] => C 4 X

人气教程排行