PHP 遍历数组的方法汇总

简介:

1. foreach()

foreach()是一个用来遍历数组中数据的最简单有效的方法。

#example1:

  1. <?php
  2. $colors= array('red','blue','green','yellow');
  3. foreach ($colorsas$color){
  4. echo "Do you like $color? <br />";
  5. }
  6. ?>


显示结果:

Do you like red? 
Do you like blue? 
Do you like green? 
Do you like yellow?

2. while()

while() 通常和 list(),each()配合使用。

#example2:

  1. <?php
  2. $colors= array('red','blue','green','yellow');
  3. while(list($key,$val)= each($colors)) {
  4. echo "Other list of $val.<br />";
  5. }
  6. ?>

显示结果:

Other list of red. 
Other list of blue. 
Other list of green. 
Other list of yellow.

3. for()

#example3:

  1. <?php
  2. $arr= array ("0"=> "zero","1"=> "one","2"=> "two");
  3. for ($i= 0;$i< count($arr); $i++){
  4. $str= $arr[$i];
  5. echo "the number is $str.<br />";
  6. }
  7. ?>

显示结果:

the number is zero. 
the number is one. 
the number is two.

========= 以下是函数介绍 ==========

key()

mixed key(array input_array)

key()函数返回input_array中位于当前指针位置的键元素。

#example4

  1. <?php
  2. $capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
  3. echo "<p>Can you name the capitals of these states?</p>";
  4. while($key= key($capitals)) {
  5. echo $key."<br />";
  6. next($capitals);
  7. //每个key()调用不会推进指针。为此要使用next()函数
  8. }
  9. ?>

显示结果:

Can you name the capitals of these states? 
Ohio 
Towa 
Arizona

reset()

mixed reset(array input_array)

reset()函数用来将input_array的指针设置回数组的开始位置。如果需要在一个脚本中多次查看或处理同一个数组,就经常使用这个函数,另外这个函数还常用于排序结束时。

#example5 - 在#example1上追加代码

  1. <?php
  2. $colors= array('red','blue','green','yellow');
  3. foreach ($colorsas$color){
  4. echo "Do you like $color? <br />";
  5. }
  6. reset($colors);
  7. while(list($key,$val)= each($colors)) {
  8. echo "$key=> $val<br />";
  9. }
  10. ?>

显示结果:

Do you like red? 
Do you like blue? 
Do you like green? 
Do you like yellow? 
0 => red 
1 => blue 
2 => green 
3 => yellow

注意:将一个数组赋值给另一个数组时会重置原来的数组指针,因此在上例中如果我们在循环内部将 $colors 赋给了另一个变量的话将会导致无限循环。 
例如将 $s1 = $colors; 添加到while循环内,再次执行代码,浏览器就会无休止地显示结果。

each()

array each(array input_array)

each()函数返回输入数组当前键/值对,并将指针推进一个位置。返回的数组包含四个键,键0和key包含键名,而键1和value包含相应的数据。如果执行each()前指针位于数组末尾,则返回FALSE。

#example6

  1. <?php
  2. $capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
  3. $s1= each($capitals);
  4. print_r($s1);
  5. ?>

显示结果:

Array ( [1] => Columbus [value] => Columbus [0] => Ohio [key] => Ohio )

current(),next(),prev(),end()

mixed current(array target_array)

current()函数返回位于target_array数组当前指针位置的数组值。与next()、prev()、和end()函数不同,current()不移动指针。 
next()函数返回紧接着放在当前数组指针的下一个位置的数组值。 
prev()函数返回位于当前指针的前一个位置的数组值,如果指针本来就位于数组的第一个位置,则返回FALSE。 
end()函数将指针移向target_array的最后一个位置,并返回最后一个元素。

#example7

  1. <?php
  2. $fruits= array("apple","orange","banana");
  3. $fruit= current($fruits); //return "apple"
  4. echo $fruit."<br />";
  5. $fruit= next($fruits); //return "orange"
  6. echo $fruit."<br />";
  7. $fruit= prev($fruits); //return "apple"
  8. echo $fruit."<br />";
  9. $fruit= end($fruits); //return "banana"
  10. echo $fruit."<br />";
  11. ?>

显示结果:

apple 
orange 
apple 
banana

=========== 下面来测试三种遍历数组的速度 ===========

一般情况下,遍历一个数组有三种方法,for、while、foreach。其中最简单方便的是foreach。下面先让我们来测试一下共同遍历一个有50000个下标的一维数组所耗的时间。

测试环境: 
Intel Core Due2 2GHz 
2GB 1067MHz DDR3 
Mac OS X 10.5.7 
Apache 2.0.59 
MySQL 5.0.41 
PHP 5.2.6

#example8

  1. <?php
  2. $arr= array();
  3. for($i= 0; $i< 50000; $i++){
  4. $arr[]= $i*rand(1000,9999);
  5. }
  6. function GetRunTime()
  7. {
  8. list($usec,$sec)=explode(" ",microtime());
  9. return ((float)$usec+(float)$sec);
  10. }
  11. ######################################
  12. $time_start= GetRunTime();
  13. for($i= 0; $i< count($arr); $i++){
  14. $str= $arr[$i];
  15. }
  16. $time_end= GetRunTime();
  17. $time_used= $time_end- $time_start;
  18. echo 'Used time of for:'.round($time_used, 7).'(s)<br /><br />';
  19. unset($str, $time_start, $time_end, $time_used);
  20. ######################################
  21. $time_start= GetRunTime();
  22. while(list($key, $val)= each($arr)){
  23. $str= $val;
  24. }
  25. $time_end= GetRunTime();
  26. $time_used= $time_end- $time_start;
  27. echo 'Used time of while:'.round($time_used, 7).'(s)<br /><br />';
  28. unset($str, $key, $val, $time_start, $time_end, $time_used);
  29. ######################################
  30. $time_start= GetRunTime();
  31. foreach($arr as$key=> $val){
  32. $str= $val;
  33. }
  34. $time_end= GetRunTime();
  35. $time_used= $time_end- $time_start;
  36. echo 'Used time of foreach:'.round($time_used, 7).'(s)<br /><br />';
  37. ?>

测试结果:

Used time of for:0.0228429(s)

Used time of while:0.0544658(s)

Used time of foreach:0.0085628(s)

经过反复多次测试,结果表明,对于遍历同样一个数组,foreach速度最快,最慢的则是while。从原理上来看,foreach是对数组副本进行操作(通过拷贝数组),而while则通过移动数组内部指标进行操作,一般逻辑下认为,while应该比foreach快(因为foreach在开始执行的时候首先把数组复制进去,而while直接移动内部指标。),但结果刚刚相反。原因应该是,foreach是PHP内部实现,而while是通用的循环结构。所以,在通常应用中foreach简单,而且效率高。在PHP5下,foreach还可以遍历类的属性。

本文转自博客园知识天地的博客,原文链接:PHP 遍历数组的方法汇总,如需转载请自行联系原博主。

相关文章
|
7月前
|
PHP
php数组随机排序
PHP中对数据进行随机排序
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
|
3月前
|
JSON PHP 数据格式
php 删掉空的数组 json数据. 空数据(false 0 ““ null)
php 删掉空的数组 json数据. 空数据(false 0 ““ null)
php 删掉空的数组 json数据. 空数据(false 0 ““ null)
|
9月前
|
存储 PHP
php使用数组存储用户数据进行登录的封装函数
php使用数组存储用户数据进行登录的封装函数
46 0
|
9月前
|
PHP
php模版引擎smarty中判断数组是否为空自动输输出
php模版引擎smarty中判断数组是否为空自动输输出
62 0
|
4月前
|
JSON PHP 数据格式
【PHP学习】—数组的定义和遍历(三)
【PHP学习】—数组的定义和遍历(三)
|
7月前
|
搜索推荐 算法 PHP
PHP 数组(Array) - 排序算法
PHP 数组(Array) - 排序算法
23 0
|
7月前
|
PHP 索引
PHP 数组(Array)
PHP 数组(Array)
30 0