
正文
php遍历网页数据 php抓取网页数据
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
PHP 怎么样遍历
第一、foreach()
foreach()是一个用来遍历数组中数据php遍历网页数据的最简单有效的方法。
?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()php遍历网页数据,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还可以遍历类的属性。
希望能够喜欢。
相关问答
Q1: 在php页面怎么遍历
$basedir = "/";
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' $file != '..'){
if (!is_dir($basedir."/".$file)) {
echo "filename: $basedir/$file br";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
Q2: 如何将数据库中的结果循环遍历输出到网页中 php jquery
对数据库取出的值遍历比较好一点:
var
data={$data};
for
(var
item
in
data)
{
$("
:radio[name="+item+"][value="+data[item]+"]").attr("checked",true);
}
上面的写法可能有错误,意思就是对数据库取出的值遍历,设置name=item且value=data[item]的checked为true。
当然也可以用模板,这样就不需要遍历,使用if标签判定value值来插入"checked",
比如:
checked
Q3: php如何遍历数组
1、在test.php文件内,使用header设置test.php执行的编码为utf8,避免输出中文的时候出现乱码。
2、在test.php文件内,创建一个测试的数组,例如,定义一个分类的数组,其对应的索引值分别为0,4,8。
3、在test.php文件内,使用array_values()方法将上一步的数据重新排序,并且从0开始,把重新排序的数组保存在$result变量中。
4、在test.php文件内,使用foreach方法遍历数组,其中$k为索引值,$v为索引值对应的数组值。
5、在test.php文件内,使用echo方法输出数组中的索引值和对应的数组值即可。
Q4: php 如何循环遍历出来以下数据?
这个里面是json数据(各级元素包含数组对象等),需要用到json_decode()函数来转化。因为题目给的是图片,没法实际给出代码解答,可以参考我以前对类似问题的解答:网页链接
望采纳,谢谢
Q5: 用PHP如何遍历一个网站下的所有网页
?php $str = file_get_contents('a.html'); $str_len = strlen($str); for($i=0; $i=$str_len; $i+=50){ $sub_str = substr($str,$i,50); echo $sub_str."\nbr"; } ?
关于php遍历网页数据和php抓取网页数据的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






