數(shù)組的遍歷是PHP一個常見的編程任務(wù),,而數(shù)組又分為一維數(shù)組,、二維數(shù)組和多維數(shù)組,。一維數(shù)組的遍歷很簡單,,直接一個for循環(huán)就可以完成,。那么二維數(shù)組和多維數(shù)組的遍歷又應(yīng)該如何實現(xiàn)呢?請看以下程序: http://www./php/php_MultiAimensionalArrayTraverse.php <?php /* * ------------------------------------------------- * Author : nowamagic * Url : www. * Date : 2011-03-09 * ------------------------------------------------- */ function arr_foreach ($arr) { if (!is_array ($arr)) { return false; } foreach ($arr as $key => $val ) { if (is_array ($val)) { arr_foreach ($val); } else { echo $val.'<br/>'; } } } $arr1 = array (1=>array(11,12,13,14=>array(141,142)),2,3,4,5); echo '<pre>'; print_r($arr1); echo '<pre>'; arr_foreach ($arr1); ?> 程序運(yùn)行結(jié)果為: Array ( [1] => Array ( [0] => 11 [1] => 12 [2] => 13 [14] => Array ( [0] => 141 [1] => 142 ) ) [2] => 2 [3] => 3 [4] => 4 [5] => 5 ) 11 12 13 141 142 2 3 4 5 程序很簡單,,可以看看遞歸在其中的妙用,。 |
|