A brief discussion on the summary of foreach usage in PHP

黄舟
Release: 2023-03-06 14:32:01
Original
1354 people have browsed it


There are many ways to use a loop to output the contents of an array in PHP. We can either use an array iteration loop, or of course we can also use the following foreach method. In daily development It is the one that is used the most. Let’s take a look at its usage first:

//语法是:
foreach(arrayName as key => value){
//do something 
}
//下面是我的一个简单的示例代码:
<?php
$arr=array();
$arr[0]=&#39;a&#39;;
$arr[1]=&#39;b&#39;;
$arr[2]=&#39;c&#39;;
$arr[3]=&#39;d&#39;;
$arr[4]=&#39;e&#39;;
$arr[5]=&#39;f&#39;;
$arr[6]=&#39;g&#39;;
$arr[7]=&#39;h&#39;;
$i=null;
foreach ($arr as $key=>$value){
    echo &#39;arr[&#39;.(++$i).&#39;]\&#39;svalue=   &#39;.$value."<BR>";
}
?>
Copy after login

The next step is the result of the operation:

arr[1]&#39;svalue= a
arr[2]&#39;svalue= b
arr[3]&#39;svalue= c
arr[4]&#39;svalue= d
arr[5]&#39;svalue= e
arr[6]&#39;svalue= f
arr[7]&#39;svalue= g
arr[8]&#39;svalue= h
Copy after login

Summary: Pay attention to the proficient use of the foreach format, and pay attention to the meaning of the internal parameters. , and the changes in variables during each loop.

The above is the detailed content of A brief discussion on the summary of foreach usage 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!