【PHP遍历的有关问题?麻烦大家帮看一下】教程文章相关的互联网学习教程文章

php若干单维数组遍历方法的比较_php技巧

代码如下://a $arr=array('a'=>'abc','b'=>123,'c'=>true); //b //$arr=range('a','d'); //1 for($i=0;$iecho $arr[$i].', '; echo ''; //2 foreach($arr as $key) echo "$key, "; echo ''; //3 foreach($arr as $key=>$val) echo "$key-$val, "; echo ''; //4 reset($arr); while($item=each($arr)){ echo $item['key'].'-'.$item['value'].', '; } echo ''; //5 reset($arr); while(list($key,$val)=each($arr)){ echo "$key-$va...

遍历指定目录下的所有目录和文件的php代码_php技巧

代码如下:<?php function listFiles($path){ $result = array(); foreach(glob($path.\\."*") as $item){ $result[strtolower($item)] = $item; if(is_dir($item)){ $result += listFiles($item); } } return $result; } $path = E:\\web\\dianle; foreach(listFiles($path) as $item){ echo $item.; } 2: scandir 读取指定目录到数组 代码如下:function listFiles($path){ $result = array(); foreach( scandir($path) as $item...

PHP遍历数组的几种方法_php技巧

PHP中遍历数组有三种常用的方法: 一、使用for语句循环遍历数组; 二、使用foreach语句遍历数组; 三、联合使用list()、each()和while循环遍历数组。 这三种方法中效率最高的是使用foreach语句遍历数组。从PHP4开始就引入了foreach结构,是PHP中专门为遍历数组而设计的语句,推荐大家使用。先分别介绍这几种方法。 一、使用for语句循环遍历数组 值得大家注意的是使用for语句循环遍历数组要求遍历的数组必须是索引数组。PHP中不仅有...

php遍历数组的方法分享_php技巧

在PHP中数组分为两类: 数字索引数组和关联数组。 其中数字索引数组和C语言中的数组一样,下标是为0,1,2… 而关联数组下标可能是任意类型,与其它语言中的hash,map等结构相似。 方法1:foreach 代码如下:$sports = array( 'football' => 'good', 'swimming' => 'very well', 'running' => 'not good'); foreach ($sports as $key => $value) { echo $key.": ".$value.""; } ?> 输出结果: football: good swimming: very well...

深入理解PHP之数组(遍历顺序)Laruence原创_php技巧【图】

经常会有人问我, PHP的数组, 如果用foreach来访问, 遍历的顺序是固定的么? 以什么顺序遍历呢? 比如: 代码如下:$arr['laruence'] = 'huixinchen'; $arr['yahoo'] = 2007; $arr['baidu'] = 2008; foreach ($arr as $key => $val) { //结果是什么? } 又比如: 代码如下:$arr[2] = 'huixinchen'; $arr[1] = 2007; $arr[0] = 2008; foreach ($arr as $key => $val) { //现在结果又是什么? } 要完全了解清楚这个问题, 我想首先应该要大...

单一index.php实现PHP任意层级文件夹遍历(Zjmainstay原创)_php实例【图】

以下是核心文件: index.php文件 代码如下:header('Content-Type:text/html charset:utf-8'); date_default_timezone_set('PRC'); $rootDir = 'listFile'; //站点根目录,装载本程序所有文件 //站点base_url设置方法: //考虑到通用性,现默认使用方法二,修改方法时注意同时修改.htaccess文件 //方法一:设置站点目录为根目录 //对应.htaccess: //#RewriteBase / // $base_url = 'http://www.listfile.com/'; //方法二:设置站点...

phpFLEA中二叉树数组的遍历输出_php技巧

但是要怎样遍历这个方法产生的二叉树数组呢?以下是我的做法: 代码如下:function preTree($cat){ foreach ($cat as $c){ ?> ">: ">: if(isset($s['childrens'])){ ?>$this->preTree($s['childrens']); ?>} ?> } } ?>

php遍历所有文件及文件夹的方法深入解析_php技巧

1.方法一: 代码如下:$dir="D:"; static $dir_list =0; static $file_list =0; function listfile($dir){global $dir_list,$file_list;$d = dir($dir); while ( $entry = $d->read()) { $tem_curnt=$dir."/".$entry; if($entry=="." || $entry=="..") continue; if ( is_dir( $tem_curnt)) { listfile($tem_curnt);echo "文件夹 ".$tem_curnt.""; $dir_list++; } elseif ( is_file($tem_curnt)) { echo "文件".$tem_curnt.""; $fil...

探讨php中遍历二维数组的几种方法详解_php技巧

代码如下://使用for循环遍历$arr2=array(array("张三","20","男"),array("李四","25","男"),array("王五","19","女"),array("赵六","25","女"));echo "姓名年龄性别";for($i=0;$iecho "";for($j=0;$j echo ""; echo $arr2[$i][$j]; echo "";}echo "";echo "";}echo "";?>//使用foreach遍历 代码如下:$arr = array('one'=>array('name'=>'张三','age'=>'23','sex'=>'男'), 'two'=>array('name'=>'李四','age'=>'43','sex'=...

基于PHP遍历数组的方法汇总分析_php技巧

1. foreach()foreach()是一个用来遍历数组中数据的最简单有效的方法。#example1: 代码如下:$colors= array('red','blue','green','yellow');foreach ($colorsas$color){echo "Do you like $color? ";}?>显示结果:Do you like red? Do you like blue? Do you like green? Do you like yellow? 2. while()while() 通常和 list(),each()配合使用。#example2: 代码如下:$colors= array('red','blue','green','yellow');while(list($k...

深入for,while,foreach遍历时间比较的详解_php技巧

这个是从别人空间里看来的,不过自己还真从来没这么做过他们三者之间的比较,今天也学习了一下。 代码如下:$arr = array();for($i = 0; $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 $str = $arr[$i];}$time_end = GetRunTime();$time_use...

使用迭代器遍历文件信息的详解_php技巧

1.迭代文件的行 代码如下: public static IEnumerable ReadLines(string fileName) { using (TextReader reader = File.OpenText(fileName)) { string line; if ((line = reader.ReadLine()) != null) { yield return line; } } } static void Main() { foreach...

解析PHPSPL标准库的用法(遍历目录,查找固定条件的文件)_php技巧

class RecursiveFileFilterIterator extends FilterIterator { // 满足条件的扩展名 protected $ext = array('jpg','gif'); /** * 提供 $path 并生成对应的目录迭代器 */ public function __construct($path) { parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path))); } /** * 检查文件扩展名是否满足条件 */ public ...

使用PHP遍历文件目录与清除目录中文件的实现详解_php技巧

今天无聊中练习了一下PHP遍历文件目录的程序,编写了以下两个程序,不过质量不是很好,轻拍~~~1、清除PHP缓存文件 代码如下:function read_dir($dir,$file) { $a =strpos($file,".php"); if($a>0) { unlink($dir . $file); echo "delete $dir$file "; return true; } if(strpos($file,".") === 0 || strpos($file,".") !== false ) return true; if(strpos($file,".")...

PHP遍历某个目录下的所有文件和子文件夹的实现代码_php技巧

代码如下: function read_all_dir ( $dir ) { $result = array(); $handle = opendir($dir); if ( $handle ) { while ( ( $file = readdir ( $handle ) ) !== false ) { if ( $file != '.' && $file != '..') { $cur_path = $dir . DIRECTORY_SEPARATOR . $file; if ( is_dir ( $cur_path ) ) ...