我们想要得到大堆数据,你就要对数组进行循环,我们现在就来看看php数组循环得到数据。因要负责将数据放置在数组内,现在,如何将其取出呢? 从数组中检索数据非常简单:所有你所需要做的就是使用索引号来访问数组的适当元素。为了读取整个数组的内容,你只需要使用你在该教程第三章中所学到的循环结构来简单的对其进行循环操作即可。
来一个快捷的例子如何?
<OL class=dp-xml><LI class=alt><SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN><SPAN class=tag></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> My favourite bands are: </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>ul</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> <br>// define array $</SPAN><SPAN class=attribute><FONT color=#ff0000>artists</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses'); <br>// loop over it and print array elements for ($</SPAN><SPAN class=attribute><FONT color=#ff0000>x</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>; $x </SPAN><SPAN class=tag><STRONG><FONT color=#006699><</FONT></STRONG></SPAN><SPAN> </SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>sizeof</FONT></STRONG></SPAN><SPAN>($artists); $x++) { echo '</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>li</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>'.$artists[$x]; } </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>ul</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN></LI></OL>当你运行该脚本时,你会看到下面的结果:
<OL class=dp-xml><LI class=alt><SPAN><SPAN>My favourite bands are: Metallica Evanescence Linkin Park Guns n Roses </SPAN></SPAN></LI></OL>
<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> // define an array $</SPAN><SPAN class=attribute><FONT color=#ff0000>menu</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('breakfast' =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 'bacon and eggs', 'lunch' =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 'roast beef', 'dinner' =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 'lasagna'); <br>/* returns the array ('breakfast', 'lunch', 'dinner') with numeric indices */ $</SPAN><SPAN class=attribute><FONT color=#ff0000>result</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array_keys</FONT></SPAN><SPAN>($menu); print_r($result); <br>print "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></FONT></STRONG></SPAN><SPAN>"; /* returns the array ('bacon and eggs', 'roast beef', 'lasagna') with numeric indices */ $</SPAN><SPAN class=attribute><FONT color=#ff0000>result</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array_values</FONT></SPAN><SPAN>($menu); <br>print_r($result); </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN></SPAN></LI></OL>然而,这里还有一种更简单的方法来提取数组中的所有元素。PHP4.0介绍了一种经设计专门用于对数组反复枚举目的的非常新的循环类型:foreach()循环(它的语法结构类似于同名的Perl结构)。
立即学习“PHP免费学习笔记(深入)”;
下面是其语法格式:
<OL class=dp-xml><LI class=alt><SPAN><SPAN>foreach ($array as $temp) { do this! } </SPAN></SPAN></LI></OL>foreach()循环对作为参数传递给它的数组的每一个元素运行一次,在每次重复时向前遍历该数组。和for()循环不同,它不需要计数器或调用函数 sizeof(),因为它自动跟踪其在数组中的位置。在每次运行的时候,执行大括号内的语句,同时,当前选择的数组元素可以通过一个临时的PHP数组循环变量来访问。
为了更好的理解它是如何工作的,考虑使用foreach()循环对之前的例子进行重新改写:
<OL class=dp-xml><LI class=alt><SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN><SPAN class=tag></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> My favourite bands are: </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>ul</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> // define array $</SPAN><SPAN class=attribute><FONT color=#ff0000>artists</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array<br></FONT></SPAN><SPAN>('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses'); // loop over it // print array elements foreach ($artists as $a)<br>{ echo '</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>li</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>'.$a; } </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>ul</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN></LI></OL>每次执行循环时,它将当前选择的数组元素的值放在临时变量$a中。之后,该变量可以被PHP数组循环块中的语句进行使用。因为foreach()循环不需要计数器跟踪其在数组中的位置,所以它需要更少的维护且同时比标准的for()循环更加易读。奥,是的…,它同样也可与关联数组一起起作用,而不需要额外的编程。











