PHP遍历数组的方法<转>

 
1. foreach()

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

#example1:

 

  1.  
  2. $colors = array('red','blue','green','yellow');
  3. foreach ($colors as $color) {
  4. echo "Do you like $color? 
    ";
  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.  
  2. $colors = array('red','blue','green','yellow');
  3.  
  4. while(list($key,$val) = each($colors)) {
  5. echo "Other list of $val.
    ";
  6. }
  7. ?>

显示结果:

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

3. for()

#example3:

 

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

显示结果:

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

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

key()

mixed key(array input_array)

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

#example4

 

  1.  
  2. $capitals = array("Ohio" => "Columbus","Towa" => "Des Moines","Arizona" => "Phoenix");
  3. echo "

    Can you name the capitals of these states?

    ";
  4. while($key = key($capitals)) {
  5. echo $key."
    ";
  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.  
  2. $colors = array('red','blue','green','yellow');
  3. foreach ($colors as $color) {
  4. echo "Do you like $color? 
    ";
  5. }
  6.  
  7. reset($colors);
  8.  
  9. while(list($key,$val) = each($colors)) {
  10. echo "$key => $val
    ";
  11. }
  12. ?>

显示结果:

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.  
  2. $capitals = array("Ohio" => "Columbus","Towa" => "Des Moines","Arizona" => "Phoenix");
  3.  
  4. $s1 = each($capitals);
  5. print_r($s1);
  6. ?>

显示结果:

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.  
  2. $fruits = array("apple","orange","banana");
  3.  
  4. $fruit = current($fruits);    //return "apple"
  5. echo $fruit."
    ";
  6.  
  7. $fruit = next($fruits);        //return "orange"
  8. echo $fruit."
    ";
  9.  
  10. $fruit = prev($fruits);        //return "apple"
  11. echo $fruit."
    ";
  12.  
  13. $fruit = end($fruits);        //return "banana"
  14. echo $fruit."
    ";
  15. ?>

显示结果:

apple 
orange 
apple 
banana

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值