Rumah >pembangunan bahagian belakang >tutorial php >PHP数组遍历语法讲解_PHP教程
PHP是一门编写WEB开发的脚本语言,对于它我想大家也有深入的理解了,这里我就PHP数组遍历和大家探讨一下,希望对大家有帮助。
PHP数组遍历语法:
<ol class="dp-xml"><li class="alt"><span><span>foreach(array_expressionas$</span><span class="attribute"><font color="#ff0000">key</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$value)statement </span></span></li></ol>
第二种格式做同样的事,只除了当前单元的键值也会在每次循环中被赋给变量$key。
例如1:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">url</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('新浪'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.sina.com', </span> </li> <li class="alt"> <span>'雅虎'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.yahoo.com', </span> </li> <li class=""> <span>'网易'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.163.com', </span> </li> <li class="alt"> <span>'腾讯'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.qq.com', </span> </li> <li class=""> <span>'百度'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.baidu.com' </span> </li> <li class="alt"><span>); </span></li> <li class=""><span> </span></li> <li class="alt"> <span>foreach($urlas$</span><span class="attribute"><font color="#ff0000">site</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$link){ </span> </li> <li class=""> <span>echo"</span><span class="tag"><strong><font color="#006699"></font></strong></span><span class="attribute"><font color="#ff0000">ahref</font></span><span class="tag-name"><strong><font color="#006699">ahref</font></strong></span><span>=</span><span class="attribute-value"><font color="#0000ff">http</font></span><span>://$link</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$site</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></span></font></strong><span>"; </span> </li> <li class="alt"><span>} </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class="alt"><span>$site得到键值(如“新浪”),$link得到该单元的值(如“www.sina.com”),我在中间夹了一些HTML代码,相信你会看懂的。下面我再加点东西你能看懂吗?例10-3: </span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">url</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('新浪'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.sina.com', </span> </li> <li class=""> <span>'雅虎'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.yahoo.com', </span> </li> <li class="alt"> <span>'网易'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.163.com', </span> </li> <li class=""> <span>'腾讯'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.qq.com', </span> </li> <li class="alt"> <span>'百度'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.baidu.com' </span> </li> <li class=""><span>); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">n</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; </span> </li> <li class=""> <span>foreach($urlas$</span><span class="attribute"><font color="#ff0000">site</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$link){ </span> </li> <li class="alt"><span>++$n; </span></li> <li class=""> <span>echo"第$n名:</span><span class="tag"><strong><font color="#006699"></font></strong></span><span class="attribute"><font color="#ff0000">ahref</font></span><span class="tag-name"><strong><font color="#006699">ahref</font></strong></span><span>=</span><span class="attribute-value"><font color="#0000ff">http</font></span><span>://$link</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$site</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></span></font></strong><span>"; </span> </li> <li class="alt"><span>} </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
注:当foreach开始执行时,数组内部的指针会自动指向第一个单元。这意味着不需要在foreach循环之前调用reset()。注:此外注意foreach所操作的是指定数组的一个拷贝,而不是该数组本身。因此即使有each()的构造,原数组指针也没有变,数组单元的值也不受影响。注:foreach不支持用“@”来禁止错误信息的能力。