Home > php教程 > php手册 > php遍历循环数组实现方法,php遍循环数组

php遍历循环数组实现方法,php遍循环数组

WBOY
Release: 2016-06-13 08:54:58
Original
1040 people have browsed it

php遍历循环数组实现方法,php遍循环数组

简单利用foreach for list each while来遍历数组,包括普通的一维数组与二维数组遍历方法,下面详细的介绍了每个函数的使用方法.

<ol class="dp-c">
<li class="alt"><span class="vars">$foreach = <span class="keyword">array(1,2,3); </span></span></li>
<li><span class="vars">$array2 = <span class="keyword">array(<span class="keyword">array(<span class="string">'aa'=>33,<span class="string">'bb'=>22,<span class="string">'www.phpfensi.com'=>11),<span class="keyword">array(<span class="string">'dd'=>44,<span class="string">'ee'=>55,<span class="string">'ff'=>66)); </span></span></span></span></span></span></span></span></span></span></li>
</ol>
Copy after login

利用for foreach遍历一维数据:

<ol class="dp-c">
<li class="alt"><span class="keyword">for( <span class="vars">$i=0;<span class="vars">$i<<span class="func">count(<span class="vars">$foreach);<span class="vars">$i++) </span></span></span></span></span></span></li>
<li>{ </li>
<li class="alt"> <span class="func">echo <span class="string">'foreach['.<span class="vars">$i.<span class="string">']='.<span class="vars">$foreach[<span class="vars">$i].<span class="string">'<br>'; </span></span></span></span></span></span></span></li>
<li>} </li>
<li class="alt">/* </li>
<li><span class="comment">//foreach[0]=1 </span></li>
<li class="alt"><span class="comment">//foreach[1]=2 </span></li>
<li><span class="comment">//foreach[2]=3 </span></li>
</ol>
Copy after login

再看利用foreach来遍历:

<ol class="dp-c">
<li class="alt"><span class="keyword">foreach( <span class="vars">$foreach <span class="keyword">as <span class="vars">$v ) </span></span></span></span></li>
<li>{ </li>
<li class="alt"> <span class="func">echo <span class="string">'foreach='.<span class="vars">$v.<span class="string">'<br>'; </span></span></span></span></li>
<li>} </li>
<li class="alt"><span class="comment">//foreach=1 </span></li>
<li><span class="comment">//foreach=2 </span></li>
<li class="alt"><span class="comment">//foreach=3 </span></li>
</ol>
Copy after login

上面用的都是遍历一维数据,下面来看二维数组:

<ol class="dp-c">
<li class="alt"><span class="keyword">foreach(<span class="vars">$array2 <span class="keyword">as <span class="vars">$key=><span class="vars">$value) </span></span></span></span></span></li>
<li>{ </li>
<li class="alt"> <span class="keyword">foreach(<span class="vars">$value <span class="keyword">as <span class="vars">$k=><span class="vars">$v) </span></span></span></span></span></li>
<li> { </li>
<li class="alt">  <span class="func">echo <span class="string">'$arr['.<span class="vars">$key.<span class="string">']['.<span class="vars">$k.<span class="string">']='.<span class="vars">$v; </span></span></span></span></span></span></span></li>
<li> } </li>
<li class="alt"> <span class="func">echo <span class="string">"<br>"; </span></span></li>
<li>} </li>
</ol>
Copy after login

看一下list each:

each() 函数生成一个由数组当前内部指针所指向的元素的键名和键值组成的数组,并把内部指针向前移动。

返回的数组中包括的四个元素:键名为 0,1,key 和 value。单元 0 和 key 包含有数组单元的键名,1 和 value 包含有数据。

如果内部指针越过了数组范围,本函数将返回 false。

语法:each(array)参数 描述

array 必需,规定要使用的数组。

list函数

list() 函数用数组中的元素为一组变量赋值。

注意,与 array() 类似,list() 实际上是一种语言结构,不是函数.

语法:list(var1,var2...)参数 描述

var1 必需,第一个需要赋值的变量。

var2 可选,可以有多个变量。

提示和注释:

注释:该函数只用于数字索引的数组,且假定数字索引从 0 开始.

<ol class="dp-c">
<li class="alt"><span class="vars">$colors = <span class="keyword">array(<span class="string">'red',<span class="string">'blue',<span class="string">'green',<span class="string">'yellow');  </span></span></span></span></span></span></li>
<li><span class="keyword">while(list(<span class="vars">$key,<span class="vars">$val) = each(<span class="vars">$colors)) {  </span></span></span></span></li>
<li class="alt">    <span class="func">echo <span class="string">"other list of $val.<br />";  </span></span></li>
<li>} </li>
<li class="alt"><span class="comment">//other list of red. </span></li>
<li><span class="comment">//other list of blue. </span></li>
<li class="alt"><span class="comment">//other list of green. </span></li>
<li><span class="comment">//other list of yellow. </span></li>
</ol>
Copy after login

for 语句:

如果您已经确定了代码块的重复执行次数,则可以使用 for 语句.

语法:

<ol class="dp-c">
<li class="alt"><span class="keyword">for (initialization; condition; increment) </span></li>
<li>{ </li>
<li class="alt">  code to be executed; </li>
<li>} </li>
</ol>
Copy after login

注释:for 语句有三个参数,第一个参数初始化变量,第二个参数保存条件,第三个参数包含执行循环所需的增量,如果 initialization 或 increment 参数中包括了多个变量,需要用逗号进行分隔,而条件必须计算为 true 或者 false.

foreach 语句用于循环遍历数组.

每进行一次循环,当前数组元素的值就会被赋值给 value 变量(数组指针会逐一地移动)- 以此类推.

Related labels:
php
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template