PHP 遍历数组语句总结(foreach,for,list,each)

在php中遍历循环读出数组的方法有几种,foreach,for,list,each,while都是可以的,只是那种更适合用来遍历数组了。

foreach来访问,遍历的顺序是固定的么? 以什么顺序遍历呢?

比如:代码如下:

  1. <?php
  2. $colors= array('red','blue','green','yellow');
  3. foreach ($colors as $color){
  4. //add your codes
  5. }
  6. ?>

例2,代码如下:

  1. $capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
  2. foreach($capitals as $key=> $val){
  3. //add your codes
  4. }

while()

while() 通常和 list(),each()配合使用,实例代码如下:

  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. ?>
  7. /*
  8. 显示结果:
  9. Other list of red.
  10. Other list of blue.
  11. Other list of green.
  12. Other list of yellow.
  13. */

3. for(),实例代码如下:

  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. ?>
  8. /*
  9. 显示结果:
  10. the number is zero.
  11. the number is one.
  12. the number is two.
  13. */

以下是函数介绍:

key()

mixed key(array input_array)

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

实例代码如下:

  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. ?>
  10. /*结果如下
  11. Can you name the capitals of these states?
  12. Ohio
  13. Towa
  14. Arizona
  15. */

each() 函数遍历数组

例子1,代码如下:

  1. <?php
  2. $people = array("Peter", "Joe", "Glenn", "Cleveland");
  3. print_r (each($people));
  4. ?>
  5. //输出:
  6. //Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )

例子2,代码如下:

each() 经常和 list() 结合使用来遍历数组,本例与上例类似,不过循环输出了整个数组,代码如下:

  1. <?php
  2. $people = array("Peter", "Joe", "Glenn", "Cleveland");
  3. reset($people);
  4. while (list($key, $val) = each($people))
  5. {
  6. echo "$key => $val<br />";
  7. }
  8. ?>
  9. /*
  10. 输出:
  11. 0 => Peter
  12. 1 => Joe
  13. 2 => Glenn
  14. 3 => Cleveland
  15. */

多维数组的递归遍历,代码如下:

  1. <?php
  2. /*
  3. * -------------------------------------------------
  4. * Author :
  5. * Url : www.phpfensi.com* Date : 2011-03-09
  6. * -------------------------------------------------
  7. */
  8. function arr_foreach ($arr)
  9. {
  10. if (!is_array ($arr))
  11. {
  12. return false;
  13. }
  14. foreach ($arr as $key => $val )
  15. {
  16. if (is_array ($val))
  17. {
  18. arr_foreach ($val);
  19. }
  20. else
  21. {
  22. echo $val.'<br/>';
  23. }
  24. }
  25. }
  26. $arr1 = array (1=>array(11,12,13,14=>array(141,142)),2,3,4,5);
  27. echo '<pre>';
  28. print_r($arr1);
  29. echo '<pre>';
  30. arr_foreach ($arr1);
  31. ?>
  32. /*
  33. 结果
  34. Array
  35. (
  36. [1] => Array
  37. (
  38. [0] => 11
  39. [1] => 12
  40. [2] => 13
  41. [14] => Array
  42. (
  43. [0] => 141
  44. [1] => 142
  45. )
  46. )
  47. [2] => 2
  48. [3] => 3
  49. [4] => 4
  50. [5] => 5
  51. )
  52. 11
  53. 12
  54. 13
  55. 141
  56. 142
  57. 2
  58. 3
  59. 4
  60. 5
  61. */