Traversing all elements in an array is a common operation. you can perform queries or other operations during the traversal process. In php learning, there are many ways to traverse arrays. The most common method is to traverse arrays through the foreach () and list () functions.
Traversing all elements in an array is a common operation. you can perform queries or other operations during the traversal process. In php learning, there are many ways to traverse arrays. The most common method is to traverse arrays through the foreach () and list () functions.
1. foreach traverses the array
For example:
$ Arr1 = array ("team" => "barcelona", "name" => "messi", "age" => 24 );
Foreach ($ arr1 as $ value ){
Echo $ value ."
";
}
In the preceding example, php executes a loop for each element of the array $ arr1, assigns $ value to each element, and processes each element in the internal order of the array.
2. use the list () function to traverse arrays.
The list () function assigns values in the array to some variables. Similar to the array () function, it is not a real function, but a language structure. The list () function can only be used as an array of numeric indexes, and the number index starts from 0.
To better understand the list () function, we use the list () function and the each () function to obtain the user login information stored in the array.
For example:
While (list ($ name, $ value) = each ($ _ POST )){
If ($ name! = "Real "){
Echo "$ name: $ value
";
}
}