Foreach traversal array
When using arrays, we often need to traverse the array and obtain each key or element value. php provides some functions dedicated to traversing the array. This section describes the usage of foreach's array Traversal function.
Structure:
Foreach (array_expression as $ value) statement
/* Array_expression is the array to be traversed
As is used to assign the value of the array to $ value.
Statement is a subsequent statement.
*/
Instance 1:
The code is as follows: |
Copy code |
<? Php $ Color = array ('White '=> 'white ', 'Black' => 'black ', 'Red' => 'red ', 'Green' => 'green ', 'Yellow' => 'yellow '); Foreach ($ color as $ c) echo $ c. "<br> "; ?> |
Using foreach, you can obtain not only the element value, but also the key name. The structure is as follows:
Foreach (array_expression as $ key => $ value) statement
Run the following code on the above instance:
The code is as follows: |
Copy code |
Foreach ($ color as $ c) echo $ c. "<br> ";
Changed: Foreach ($ color as $ key => $ c) echo $ key. $ c. "<br> "; |
Each traversal array
Traversing arrays is an important part of php Array Operations. In addition to the foreach function mentioned above, we will introduce an array Traversal function-each ().
The each () function can be used to output the key name and corresponding element value at the current pointer position. You can use "0 & Prime; or" key "to access the key name (identifier), and use" 1 & Prime; or "value" to access the value corresponding to the identifier.
Instance:
The code is as follows: |
Copy code |
<? Php $ Ages = array (1 => "php ", 5 => "html ", 10 => "css "); $ A = each ($ ages);/* traverse the array for the first time */ Echo $ a [0]. "t "; Echo $ a [1]. "<br> "; $ A = each ($ ages);/* The second traversal array */ Echo $ a [key]. "t "; Echo $ a [value]; ?> |
List traversal array
The function list can be assigned to the variable once when the array is traversed. It is usually used with the each () function. Using the list () function makes it easier to access keys and values returned by each.
Instance:
The code is as follows: |
Copy code |
<? Php $ Date = array (1 => "Monday ", 2 => "Tuesday ", 3 => "Wednesday "); List ($ key, $ value) = each ($ date);/* Traversal function */ Echo "$ key $ value". "<br>";/* output the first array */ $ Next = next ($ date);/* move the pointer behind */ Echo "$ next "; ?> |
Ps: The list () function is just opposite to the array () function. array () constructs a series of data into an array, while list () splits the array into data.
For traversal array
In addition to some predefined php array traversal functions, we can also use the loop feature of the for statement to output the array traversal. The following is an example:
The code is as follows: |
Copy code |
<? Php $ A [] = "Zhang Xueyou";/* define an array */ $ A [] = "Andy Lau "; $ A [] = "Dawn "; $ A [] = "Guo Fucheng "; $ S = count ($ a);/* count the number of arrays */ For ($ I = 0; $ I <$ s; $ I ++) {/* traverse the array */ Echo $ a [$ I]. "<br>";/* Display array */ } ?> |