PHP数组遍历基础方法
1. foreach() foreach()是遍历数组数据最简单且高效的方法。 #example1: 复制代码 代码如下:
2. while() while()通常与list()和each()配合使用。 #example2: 复制代码 代码如下:
3. for() #example3: 复制代码 代码如下:
========= 以下是函数介绍 ========== key() mixed key(array input_array) key()函数返回input_array中当前指针位置指向的键元素。 #example4 复制代码 代码如下:
Can you name the capitals of these states?
";while($key= key($capitals)) {
echo $key.
";
next($capitals);
//每个key()调用不会推进指针。为此要使用next()函数
}
?>
显示结果:
Can you name the capitals of these states?
Ohio
Towa
Arizona
reset()
mixed reset(array input_array)
reset()函数用于将input_array的指针重置回数组起始位置。若需在脚本中多次处理同一数组,常使用该函数;此外,排序结束时也常调用此函数。
#example5 - 在#example1上追加代码
<?php
$colors= array('red','blue','green','yellow');
foreach ($colors as $color){
echo "Do you like $color?"
";
}
reset($colors);
while(list($key,$val)= each($colors)) {
echo "$key=> $val"
";
}
?>显示结果:
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存对应数据。若执行前指针已在末尾,则返回FALSE。
#example6
<?php
$capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
$s1= each($capitals);
print_r($s1);
?>显示结果:
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
<?php
$fruits= array("apple","orange","banana");
$fruit= current($fruits); //return "apple"
echo $fruit.
";
$fruit= next($fruits); //return "orange"
echo $fruit.
";
$fruit= prev($fruits); //return "apple"
echo $fruit.
";
$fruit= end($fruits); //return "banana"
echo $fruit.
";
?>显示结果:
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
<?php
$arr= array();
for($i= 0; $i< 50000; $i++){
$arr[]= $i*rand(1000,9999);
}
function GetRunTime()
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec+(float)$sec);
}
######################################
$time_start= GetRunTime();
for($i= 0; $i< count($arr); $i++){
$str= $arr[$i];
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo 'Used time of for:'.round($time_used, 7).'(s)
';
unset($str, $time_start, $time_end, $time_used);
######################################
$time_start= GetRunTime();
while(list($key, $val)= each($arr)){
$str= $val;
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo 'Used time of while:'.round($time_used, 7).'(s)
';
unset($str, $key, $val, $time_start, $time_end, $time_used);
######################################
$time_start= GetRunTime();
foreach($arr as $key=> $val){
$str= $val;
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo 'Used time of foreach:'.round($time_used, 7).'(s)
';
?>测试结果:
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还可以遍历类的属性。