PHP遍历数组的三种方法及效率对比分析
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
本文实例分析了PHP遍历数组的三种方法及效率对比。分享给大家供大家参考。具体分析如下:
今天有个朋友问我一个问题php遍历数组的方法,告诉她了几个。顺便写个文章总结下,如果总结不全还请朋友们指出
第一、foreach()
foreach()是一个用来遍历数组中数据的最简单有效的方法。
<?php $urls= array('aaa','bbb','ccc','ddd'); foreach ($urls as $url){ echo "This Site url is $url! <br />"; } ?>
显示结果:
This Site url is aaa This Site url is bbb This Site url is ccc This Site url is ddd
第二、while() 和 list(),each()配合使用。
<?php $urls= array('aaa','bbb','ccc','ddd'); while(list($key,$val)= each($urls)) { echo "This Site url is $val.<br />"; } ?>
显示结果:
This Site url is aaa This Site url is bbb This Site url is ccc This Site url is ddd
第三、for()运用for遍历数组
<?php $urls= array('aaa','bbb','ccc','ddd'); for ($i= 0;$i< count($urls); $i++){ $str= $urls[$i]; echo "This Site url is $str.<br />"; } ?>
显示结果:
This Site url is aaa This Site url is bbb This Site url is ccc This Site url is ddd
有时候有人也在问这几种遍历数组的方法哪个更快捷些呢,下面做个简单的测试就明白了
下面来测试三种遍历数组的速度
一般情况下,遍历一个数组有三种方法,for、while、foreach。其中最简单方便的是foreach。下面先让我们来测试一下共同遍历一个有50000个下标的一维数组所耗的时间。
<?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)<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 />'; ?>
测试结果:
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程序设计有所帮助。
相关文章
详解php中implode explode serialize json msgpack性能对比
这篇文章主要介绍了php中implode/explode、serialize、json、 msgpack性能对比,对性能感兴趣的同学,可以参考下2021-04-04PHP的substr_replace将指定两位置之间的字符替换为*号
PHP的substr_replace将指定两位置之间的字符替换为*号的代码,需要的朋友可以参考下。2011-05-05PHP中$GLOBALS[''HTTP_RAW_POST_DATA'']和$_POST的区别分析
这篇文章主要介绍了PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的区别,结合具体实例形式分析了$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的功能与使用过程中的区别,需要的朋友可以参考下2017-07-07关于file_get_contents返回为空或函数不可用的解决方案
本篇文章是对file_get_contents返回为空或函数不可用的解决方案进行了详细的分析介绍,需要的朋友参考下2013-06-06
最新评论