Heim >Backend-Entwicklung >PHP-Tutorial >PHP数组遍历语法讲解_PHP教程

PHP数组遍历语法讲解_PHP教程

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-15 13:27:56980Durchsuche

PHP是一门编写WEB开发的脚本语言,对于它我想大家也有深入的理解了,这里我就PHP数组遍历和大家探讨一下,希望对大家有帮助。

我们大家都认识到了数组的优越性,现在我们将学习怎样PHP数组遍历中的数据。这种操作数组方法的应用是非常普遍的,我相信你会掌握得很好。比如你的网站存有大量的网址数据,你想在一个页面显示门户网站网址,这些数据已经保存到了一个数组变量$url里,那么怎样把这些数据一个一个输出到网页里呢?你还想象上节课那样一个一个echo输出吗?别忘了数据量是非常大而且数据的个数是经常变化的噢。

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不支持用“@”来禁止错误信息的能力。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446498.htmlTechArticlePHP是一门编写WEB开发的脚本语言,对于它我想大家也有深入的理解了,这里我就PHP数组遍历和大家探讨一下,希望对大家有帮助。 我们大家...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn