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]='a'; $arr[1]='b'; $arr[2]='c'; $arr[3]='d'; $arr[4]='e'; $arr[5]='f'; $arr[6]='g'; $arr[7]='h'; $i=null; foreach ($arr as $key=>$value){ echo 'arr['.(++$i).']\'svalue= '.$value."<BR>"; } ?>
The next step is the result of the operation:
arr[1]'svalue= a arr[2]'svalue= b arr[3]'svalue= c arr[4]'svalue= d arr[5]'svalue= e arr[6]'svalue= f arr[7]'svalue= g arr[8]'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!