Home  >  Article  >  php教程  >  PHP learning to traverse arrays

PHP learning to traverse arrays

黄舟
黄舟Original
2016-12-20 11:50:271404browse

Traversing all elements in an array is a common operation, and queries or other operations can be completed during the traversal process. Let me introduce to you that there are many ways to traverse arrays in PHP learning. The most commonly used ones are 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 above example, php executes a loop for each element of the array $arr1 in sequence, and assigns $value to each current element. Processed in order within the array.
2. Use the list() function to traverse the array
The list() function assigns the values ​​in the array to some variables. Similar to the array() function, it is not a real function, but a language construct. The list() function can only be used on numerically indexed arrays, and numerical indexing starts from 0.
In order to better understand the list() function, we use the list() function combined with the each() function to obtain the user login information stored in the array.
For example:
while(list($name,$value) =each($_POST)){
                                                                           }

The above is the content of traversing arrays in PHP learning. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn