PHP traversal array statement summary (foreach, for, list, each ). For foreach access, is the traversal order fixed? In what order does it traverse? For example, the code is as follows? Php $ colorsarray (red, blue, green, yellow); foreach ($ colorsas $ color) {foreach for access. is the traversal order fixed? In what order does it traverse?
For example:
The code is as follows:
$ Colors = array ('red', 'blue', 'green', 'yellow ');
Foreach ($ colors as $ color ){
// Add your codes
}
?>
Example 2
$ Capitals = array ("Ohio" => "Columbus", "Towa" => "Des Moines", "Arizona" => "Phoenix ");
Foreach ($ capitals as $ key => $ val ){
// Add your codes
}
While ()
While () is usually used with list () and each.
# Example2:
The code is as follows:
$ Colors = array ('red', 'blue', 'green', 'yellow ');
While (list ($ key, $ val) = each ($ colors )){
Echo "Other list of $ val.
";
}
?>
Display result:
Other list of red.
Other list of blue.
Other list of green.
Other list of yellow.
3. ()
# Example3:
The code is as follows:
$ Arr = array ("0" => "zero", "1" => "one", "2" => "two ");
For ($ I = 0; $ I <count ($ arr); $ I ++ ){
$ Str = $ arr [$ I];
Echo "the number is $ str.
";
}
?>
Display result:
The number is zero.
The number is one.
The number is two.
========== The following is a function introduction ============
Key ()
Mixed key (array input_array)
The key () function returns the key element at the current pointer position in input_array.
# Example4
The code is as follows:
$ Capitals = array ("Ohio" => "Columbus", "Towa" => "Des Moines", "Arizona" => "Phoenix ");
Echo"
Can you name the capitals of these states?
";
While ($ key = key ($ capitals )){
Echo $ key ."
";
Next ($ capitals );
// Each key () call does not push the pointer. Use the next () function.
}
?>
Can you name the capitals of these states?
Ohio
Towa
Arizona
The each () function traverses an array.
Example 1
The code is as follows:
$ People = array ("Peter", "Joe", "Glenn", "Cleveland ");
Print_r (each ($ people ));
?>
Output:
Array ([1] => Peter [value] => Peter [0] => 0 [key] => 0)
Sub2
Each () is often used in combination with list () to traverse arrays. This example is similar to the previous example, but the entire array is output cyclically:
The code is as follows:
$ People = array ("Peter", "Joe", "Glenn", "Cleveland ");
Reset ($ people );
While (list ($ key, $ val) = each ($ people ))
{
Echo "$ key => $ val
";
}
?>
Output:
0 => Peter
1 => Joe
2 => Glenn
3 => Cleveland
Recursive traversal of multi-dimensional arrays
The code is as follows:
/*
*-------------------------------------------------
* Author:
* Url: www.45it.com * 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 .'
';
}
}
}
$ Arr1 = array (1 => array (11,12, 13,14 => array (141,142 );
Echo'
';
Print_r ($ arr1 );
Echo'';Arr_foreach ($ arr1 );
?>
Result
Array
(
[1] => Array
(
[0] => 11
[1] => 12
[2] => 13
[14] => Array
(
[0] = & gt; 141
[1] => 142.
)
)
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
11
12
13
141
142
2
3
4
5
Is the traversal order fixed? In what order does it traverse? For example, the code is as follows? Php $ colors = array ('red', 'blue', 'green', 'yellow'); foreach ($ colors as $ color ){//...