这篇文章主要为大家详细介绍了PHP遍历数组的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!
1. foreach()
foreach()是一个用来遍历数组中数据的最简单有效的方法。
#example1:
代码如下:
<?php
/* 四海网 www.q1010.com */
$colors= array('red','blue','green','yellow');
foreach ($colorsas$color){
echo "Do you like $color? <br />";
}
?>
2. while()
while() 通常和 list(),each()配合使用。
#example2:
代码如下:
<?php
/* 四海网 www.q1010.com */
$colors= array('red','blue','green','yellow');
while(list($key,$val)= each($colors)) {
echo "Other list of $val.<br />";
}
?>
3. for()
#example3:
代码如下:
<?php
/* 四海网 www.q1010.com */
$arr= array ("0"=> "zero","1"=> "one","2"=> "two");
for ($i= 0;$i< count($arr); $i++){
$str= $arr[$i];
echo "the number is $str.<br />";
}
?>
========= 以下是函数介绍 ==========
key()
mixed key(array input_array)
key()函数返回input_array中位于当前指针位置的键元素。
#example4
代码如下:
<?php
/* 四海网 www.q1010.com */
$capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
echo "<p>Can you name the capitals of these states?</p>";
while($key= key($capitals)) {
echo $key."<br />";
next($capitals);
//每个key()调用不会推进指针。为此要使用next()函数
}
?>
代码如下:
<?php
/* 四海网 www.q1010.com */
$colors= array('red','blue','green','yellow');
foreach ($colorsas$color){
echo "Do you like $color? <br />";
}
reset($colors);
while(list($key,$val)= each($colors)) {
echo "$key=> $val<br />";
}
?>
代码如下:
<?php
/* 四海网 www.q1010.com */
$capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
$s1= each($capitals);
print_r($s1);
?>
代码如下:
<?php
/* 四海网 www.q1010.com */
$fruits= array("apple","orange","banana");
$fruit= current($fruits); //return "apple"
echo $fruit."<br />";
$fruit= next($fruits); //return "orange"
echo $fruit."<br />";
$fruit= prev($fruits); //return "apple"
echo $fruit."<br />";
$fruit= end($fruits); //return "banana"
echo $fruit."<br />";
?>
代码如下:
<?php
/* 四海网 www.q1010.com */
$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)<br /><br />';
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)<br /><br />';
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)<br /><br />';
?>
本文来自:http://www.q1010.com/173/15539-0.html
注:关于PHP遍历数组的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:遍历数组
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。