本文目錄一覽:
php開發中如何實現無限遍歷目錄
$tree = ”;
do {
$tree .= ‘/*’;
$files = glob(__DIR__ . $tree);
if ($files) {
foreach ($files as $file) {
if (
pathinfo($file, PATHINFO_EXTENSION) == ‘php’
and
! in_array(substr(pathinfo($file, PATHINFO_BASENAME), 0, 1), [‘.’, ‘_’])
) {
require $file;
}
}
unset($file);
}
} while (isset($files) and $files);
unset($tree, $files);
這是我隨便寫的一個示例,無限遍歷當前目錄的,你可以參考一下。
php數組遍歷類與用法示例
本文實例講述了php數組遍歷類與用法。分享給大家供大家參考,具體如下:
?php
class
scanArray{
public
$arr;
public
$where;
private
$str;
public
function
scan($arr,$where=”array”){
$this-arr
=
$arr;
$this-where
=
$where;
foreach($this-arr
as
$k=$v){
if(is_array($v)){
$this-where
=
($this-where).”[{$k}]”;
$this-scan($v,$this-where);
}else{
$this-str
.=
$this-where.”[{$k}]=”.$v.’br
/’;
}
}
return
$this-str;
}
function
__destruct(){
unset($this-arr);
unset($this-where);
}
}
$a
=
array(‘g’=”a”,’vv’=array(“b”=”b”,”l”=”c”,”xx”=array(“e”,”g”)));
$ah
=
new
scanArray();
$b
=
$ah-scan($a);
echo
$b;
運行結果:
array[g]=a
array[vv][b]=b
array[vv][l]=c
array[vv][xx][0]=e
array[vv][xx][1]=g
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP數組(Array)操作技巧大全》、《php排序算法總結》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《php字符串(string)用法總結》及《PHP常用遍歷算法與技巧總結》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:PHP遍曆數組的方法匯總PHP
數組遍歷方法大全(foreach,list,each)PHP
數組遍歷foreach語法結構及實例PHP中多維數組的foreach遍歷示例php實現遍歷多維數組的方法PHP中使用foreach()遍歷二維數組的簡單實例PHP遍曆數組的三種方法及效率對比分析PHP實現的操作數組類庫定義與用法示例PHP數組操作類實例PHP數組生成XML格式數據的封裝類實例
php寫一個函數,能夠遍歷一個文件夾下的所有文件和子文件夾
最近剛寫的,可以遍歷指定目錄下的所有文件、文件夾、特定後綴的文件:
/**
* 遍歷目錄
* @param string $dir 絕對/相對路徑
* @param string $filter 默認*返回所有文件及文件夾,*.php僅返回php文件,如果$patten為GLOB_BRACE可實現多文件篩選,如*.{php,html},返回php和html文件
* @param const $patten 默認GLOB_BRACE,可選:GLOB_ONLYDIR,更多參數請參考手冊
* @param string/bool $nocache 防止本次調用的結果緩存上次的結果,如果一個腳本僅調用一次本函數,則不用管,否則得設個值
* @return array
*/
function globdir($dir, $filter = ‘*’, $patten = GLOB_BRACE, $nocache = null) {
static $file_arr = array ();
isset($nocache) $file_arr = array ();
if (!is_dir($dir)) return;
if ($patten == GLOB_ONLYDIR) {
$code = ‘if (is_dir($file)) {$file_arr[] = $file;globdir($file, “*”, GLOB_ONLYDIR);}’;
} else {
$code = ‘is_file($file) ? $file_arr[] = $file : globdir($file,”‘ . $filter . ‘”,’ . $patten . ‘);’;
}
array_walk(glob(“{$dir}/{$filter}”, $patten), create_function(‘$file, $k, $file_arr’, $code), $file_arr);
if ($filter != ‘*’) {
array_walk(glob(“{$dir}/*”, GLOB_ONLYDIR), create_function(‘$dir,$k,$param’, ‘list($filter, $patten) = explode(“|”, $param);globdir($dir, $filter, $patten);’), “{$filter}|{$patten}”);
}
return $file_arr;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/271523.html