$value){//execute code}" statement traversal."/> $value){//execute code}" statement traversal.">
Home > Article > Backend Development > How to use foreach to traverse an array in php
Foreach method to traverse the array: 1. Use the "foreach (array expression as $value) {//execution code}" statement to traverse; 2. Use "foreach (array expression as $key=> $value){//Execute code}" statement traversal.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you rarely need to do it yourself A large amount of data is declared in an array, but an array is obtained by calling a function return value or database query result. The purpose of using an array is to organize multiple interrelated data together to form a set and use it as a unit to achieve the purpose of batch processing of data. Most arrays require traversal to process each element in the array.
foreach is a statement in PHP designed specifically for traversing arrays. Much like Perl and other languages, it is a convenient way to traverse arrays.
When using the foreach statement to traverse an array, it has nothing to do with the subscript of the array. Whether it is a continuous index array or an associative array with a string as the subscript, you can use the foreach statement to traverse. foreach can only be applied to arrays, and since PHP5, it can also iterate over objects.
The foreach statement has two syntax forms, the second is a minor but useful extension of the first.
//第一种格式 foreach (array_expression as $value){ statement } //第二种格式 foreach (array_expression as $key => $value){ statement }
The first format loops through the given array_expression array. In each loop, the value of the current array element is assigned to $value, and the pointer inside the array moves forward one step (so the next array element will be obtained in the next loop) until the end of the array is traversed, the traversal stops and Exit the loop.
The second format has the same function as the first, except that the key name of the current array element will also be assigned to the variable $key in each loop.
Tip: The names of $key and $value are not fixed. We can define them according to our preferences when using them.
The sample code is as follows:
<?php $array = array('C语言中文网','PHP 教程','PHP 数组','http://c.biancheng.net/php/'); $num = 0; foreach ($array as $value) { echo '数组第'.$num.'个元素的值是:'.$value.'<br>'; $num++; } ?>
The above code uses the first format of foreach. If you use the second format, you can omit the variable $num. The code is as follows Display:
<?php $array = array('php中文网','PHP 教程','PHP 数组','https://www.php.cn'); foreach ($array as $key => $value) { echo '数组第'.$key.'个元素的值是:'.$value.'<br>'; } ?>
The running results of these two sets of codes are the same, as shown below:
数组第0个元素的值是:php中文网 数组第1个元素的值是:PHP 教程 数组第2个元素的值是:PHP 数组 数组第3个元素的值是:https://www.php.cn
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to use foreach to traverse an array in php. For more information, please follow other related articles on the PHP Chinese website!