In the PHP array classification of many kinds, such as a dimensional array, two-dimensional array, three-dimensional or multidimensional array, let me introduce you to PHP traversal multidimensional array, the need for friends to refer to.
In the PHP traversal array There are three functions foreach, each, list & each below we see how they traverse the multidimensional array.
Let's look at a two-dimensional array.
| The code is as follows |
Copy Code |
$sports = Array ( ' Football ' = ' good ', ' Swimming ' = ' very well ', ' Running ' = ' not good ' );
foreach ($sports as $key = = $value) { echo $key. ":". $value. " "; } ?> |
Let's look at the three-dimensional array
Example 1
| The code is as follows |
Copy Code |
function Arr_fun ($arr) { if (Is_array ($arr)) { foreach ($arr as $v) { if (Is_array ($v)) { Arr_fun ($v); }else{ echo $v. " "; } } }else{ echo $arr. " "; } } |
Example 2
| The code is as follows |
Copy Code |
!--? php /* *------------------------------------------------- * Url:www.bKjia.c0m * 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. ' '; } } } $arr 1 = Array (1=>array (11,12,13,14=>array (141,142)), 2,3,4,5); Echo ' '; Print_r ($arr 1); Echo ''; Arr_foreach ($arr 1); ?>Output results Array ( [1] = = Array ( [0] = 11 [1] = 12 [2] = 13 [+] = Array ( [0] = 141 [1] = 142 ) ) [2] = 2 [3] = 3 [4] = 4 [5] = 5 ) 11 12 13 141 142 2 3 4 5
|
Using list and each to traverse a two-dimensional array
| The code is as follows |
Copy Code |
$sports = Array ( ' Football ' = ' good ', ' Swimming ' = ' www.bKjia.c0m ', ' Running ' = ' not good ' ); while (list ($key, $value) = each ($sports)) { echo $key. ":". $value. " "; } ?> |
http://www.bkjia.com/PHPjc/633076.html www.bkjia.com true http://www.bkjia.com/PHPjc/633076.html techarticle in the PHP array classification of many kinds, such as a dimensional array, two-dimensional array, three-dimensional or multidimensional array, let me introduce you to PHP traversal multidimensional array, the need for friends to refer to. Traverse in PHP ...