多维数组的变异遍历
<?php |
02 |
/* |
03 |
* ------------------------------------------------- |
04 |
* Author : nowamagic |
05 |
* Url : www.nowamagic.net |
06 |
* Date : 2011-03-09 |
07 |
* ------------------------------------------------- |
08 |
*/ |
09 |
function arr_foreach ( $arr ) |
10 |
{ |
11 |
if (! is_array ( $arr )) |
12 |
{ |
13 |
return false; |
14 |
} |
15 |
|
16 |
foreach ( $arr as $key => $val ) |
17 |
{ |
18 |
if ( is_array ( $val )) |
19 |
{ |
20 |
arr_foreach ( $val ); |
21 |
} |
22 |
else |
23 |
{ |
24 |
echo $val . '<br/>' ; |
25 |
} |
26 |
} |
27 |
} |
28 |
29 |
$arr1 = array (1=> array (11,12,13,14=> array (141,142)),2,3,4,5); |
30 |
31 |
echo '<pre>' ; |
32 |
print_r( $arr1 ); |
33 |
echo '<pre>' ; |
34 |
35 |
arr_foreach ( $arr1 ); |
36 |
?> |
程序运行结果为:
01 |
Array |
02 |
( |
03 |
[1] => Array |
04 |
( |
05 |
[0] => 11 |
06 |
[1] => 12 |
07 |
[2] => 13 |
08 |
[14] => Array |
09 |
( |
10 |
[0] => 141 |
11 |
[1] => 142 |
12 |
) |
13 |
14 |
) |
15 |
16 |
[2] => 2 |
17 |
[3] => 3 |
18 |
[4] => 4 |
19 |
[5] => 5 |
20 |
) |
21 |
11 |
22 |
12 |
23 |
13 |
24 |
141 |
25 |
142 |
26 |
2 |
27 |
3 |
28 |
4 |
29 |
5 |