我们想要得到大堆数据,你就要对数组进行循环,我们现在就来看看PHP数组循环得到数据。因要负责将数据放置在数组内,现在,如何将其取出呢? 从数组中检索数据非常简单:所有你所需要做的就是使用索引号来访问数组的适当元素。为了读取整个数组的内容,你只需要使用你在该教程第三章中所学到的循环结构来简单的对其进行循环操作即可。
来一个快捷的例子如何?
<ol class="dp-xml"><li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span></span></font></strong><span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span></span></font></strong><span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></font></strong><span> My favourite bands are: </span><strong><font color="#006699"><span class="tag"><span class="tag-name">ul</span><span class="tag">></span></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 class="tag-name">li</span><span class="tag">></span></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 class="tag-name">br</span></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结构)。
下面是其语法格式:
<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 class="tag-name">html</span><span class="tag">></span></span></font></strong><span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span></span></font></strong><span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></font></strong><span> My favourite bands are: </span><strong><font color="#006699"><span class="tag"><span class="tag-name">ul</span><span class="tag">></span></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 class="tag-name">li</span><span class="tag">></span></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()循环更加易读。奥,是的…,它同样也可与关联数组一起起作用,而不需要额外的编程。