Home > Backend Development > PHP Problem > How to use foreach to traverse an array in php

How to use foreach to traverse an array in php

青灯夜游
Release: 2023-03-10 10:04:01
Original
3137 people have browsed it

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.

How to use foreach to traverse an array in php

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
}
Copy after login

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(&#39;C语言中文网&#39;,&#39;PHP 教程&#39;,&#39;PHP 数组&#39;,&#39;http://c.biancheng.net/php/&#39;);
    $num = 0;
    foreach ($array as $value) {
       echo &#39;数组第&#39;.$num.&#39;个元素的值是:&#39;.$value.&#39;<br>&#39;;
       $num++;
    }
?>
Copy after login

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(&#39;php中文网&#39;,&#39;PHP 教程&#39;,&#39;PHP 数组&#39;,&#39;https://www.php.cn&#39;);
   
    foreach ($array as $key => $value) {
       echo &#39;数组第&#39;.$key.&#39;个元素的值是:&#39;.$value.&#39;<br>&#39;;
    }
?>
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template