Home >Backend Development >PHP Tutorial >A brief discussion on the summary of foreach usage in PHP

A brief discussion on the summary of foreach usage in PHP

黄舟
黄舟Original
2017-03-11 10:44:361398browse


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>";
}
?>

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

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!

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