PHP多维数组的递归遍历

<?php
/*
*  -------------------------------------------------
*   Author : nowamagic
*   Url    : www.nowamagic.net
*   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);
?>

运行结果

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

你可能感兴趣的:(PHP多维数组的递归遍历)