In php, there are many functions that can be used to traverse arrays, such as the for statement, list, each, and foreach functions. these are also the main functions used to traverse arrays in php, next I will introduce it to you. In php, there are many functions that can be used to traverse arrays, such as the for statement, list, each, and foreach functions. these are also the main functions used to traverse arrays in php, next I will introduce it to you. 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: |
|
$ Color = array ('White '=> 'white ', 'Black' => 'black ', 'Red' => 'red ', 'Green' => 'green ', 'Yellow' => 'yellow '); Foreach ($ color as $ c) echo $ c ." "; ?> |
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: |
|
Foreach ($ color as $ c) echo $ c ." ";
Changed: Foreach ($ color as $ key => $ c) echo $ key. $ c ." "; |
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" or "key" to access the key name (identifier), and use "1" or "value" to access the value corresponding to the identifier.
Instance:
The code is as follows: |
|
$ 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]." "; $ 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: |
|
$ Date = array (1 => "Monday ", 2 => "Tuesday ", 3 => "Wednesday "); List ($ key, $ value) = each ($ date);/* Traversal function */ Echo "$ key $ value "." ";/* 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: |
|
$ 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]." ";/* Display array */ } ?> |