Home  >  Article  >  Backend Development  >  一个关于算法的,请大伙帮帮忙

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

WBOY
WBOYOriginal
2016-06-13 11:57:32842browse

一个关于算法的,请大家帮帮忙
$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(<br />  array( "A",  "B",  "C"),<br />  array("1",  "2",  "3", "4"),<br />  array("X",  "Y",  "Z"),<br />);<br /> <br />function foo($d) {<br />  $r = array_pop($d);<br />  while($d) {<br />    $t = array();<br />    foreach(array_pop($d) as $k1=>$x) {<br />      foreach($r as $k2=>$y) $t[] = "$x $y";<br />    }<br />    $r = $t;<br />  }<br />  return $r;<br />}<br />print_r(foo($arr));<br />

Array<br>(<br>    [0] => A 1 X<br>    [1] => A 1 Y<br>    [2] => A 1 Z<br>    [3] => A 2 X<br>    [4] => A 2 Y<br>    [5] => A 2 Z<br>    [6] => A 3 X<br>    [7] => A 3 Y<br>    [8] => A 3 Z<br>    [9] => A 4 X<br>    [10] => A 4 Y<br>    [11] => A 4 Z<br>    [12] => B 1 X<br>    [13] => B 1 Y<br>    [14] => B 1 Z<br>    [15] => B 2 X<br>    [16] => B 2 Y<br>    [17] => B 2 Z<br>    [18] => B 3 X<br>    [19] => B 3 Y<br>    [20] => B 3 Z<br>    [21] => B 4 X<br>    [22] => B 4 Y<br>    [23] => B 4 Z<br>    [24] => C 1 X<br>    [25] => C 1 Y<br>    [26] => C 1 Z<br>    [27] => C 2 X<br>    [28] => C 2 Y<br>    [29] => C 2 Z<br>    [30] => C 3 X<br>    [31] => C 3 Y<br>    [32] => C 3 Z<br>    [33] => C 4 X<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn