For foreach access, is the traversal order fixed? In what order does it traverse?
For example:
| The code is as follows: |
Copy code |
|
<? Php
$ 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: |
Copy code |
|
<? Php
$ Colors = array ('red', 'blue', 'green', 'yellow ');
While (list ($ key, $ val) = each ($ colors )){
Echo "Other list of $ val. <br/> ";
}
?>
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: |
Copy code |
|
<? Php
$ Arr = array ("0" => "zero", "1" => "one", "2" => "two ");
For ($ I = 0; $ I <count ($ arr); $ I ++ ){
$ Str = $ arr [$ I];
Echo "the number is $ str. <br/> ";
}
?>
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: |
Copy code |
|
<? Php
$ Capitals = array ("Ohio" => "Columbus", "Towa" => "Des Moines", "Arizona" => "Phoenix ");
Echo "<p> Can you name the capitals of these states? </P> ";
While ($ key = key ($ capitals )){
Echo $ key. "<br/> ";
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: |
Copy code |
|
<? Php
$ 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: |
Copy code |
|
<? Php
$ People = array ("Peter", "Joe", "Glenn", "Cleveland ");
Reset ($ people );
While (list ($ key, $ val) = each ($ people ))
{
Echo "$ key => $ val <br/> ";
}
?> |
Output:
0 => Peter
1 => Joe
2 => Glenn
3 => Cleveland
Recursive traversal of multi-dimensional arrays
| The code is as follows: |
Copy code |
|
<? Php
/*
*-------------------------------------------------
* Author:
* Url: www.111cn.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 );
Echo '<pre> ';
Print_r ($ arr1 );
Echo '<pre> ';
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 |