PHP中深度递增数组的动态生成
我想使用一个函数递归扫描文件夹,并将每次扫描的内容分配给一个数组 使用next()或foreach递归数组中的每个连续索引非常简单,但是如何向数组动态添加一层深度(而不将其硬编码到函数中)给我带来了一些问题。以下是一些伪消息:PHP中深度递增数组的动态生成,php,arrays,recursion,Php,Arrays,Recursion,我想使用一个函数递归扫描文件夹,并将每次扫描的内容分配给一个数组 使用next()或foreach递归数组中的每个连续索引非常简单,但是如何向数组动态添加一层深度(而不将其硬编码到函数中)给我带来了一些问题。以下是一些伪消息: function myScanner($start){ static $files = array(); $files = scandir($start); //do some filtering here to omit unwanted t
function myScanner($start){
static $files = array();
$files = scandir($start);
//do some filtering here to omit unwanted types
$next = next($files);
//recurse scan
//PROBLEM: how to increment position in array to store results
//$next_position = $files[][][].... ad infinitum
//myScanner($start.DIRECTORY_SEPARATOR.$next);
}
有什么想法吗?尝试使用此功能(并根据您的需求进行编辑):
它返回已排序的目录数组。请尝试以下操作:
// $array is a pointer to your array
// $start is a directory to start the scan
function myScanner($start, &$array){
// opening $start directory handle
$handle = opendir($start);
// now we try to read the directory contents
while (false !== ($file = readdir($handle))) {
// filtering . and .. "folders"
if ($file != "." && $file != "..") {
// a variable to test if this file is a directory
$dirtest = $start . DIRECTORY_SEPARATOR . $file;
// check it
if (is_dir($dirtest)) {
// if it is the directory then run the function again
// DIRECTORY_SEPARATOR here to not mix files and directories with the same name
myScanner($dirtest, $array[$file . DIRECTORY_SEPARATOR]);
} else {
// else we just add this file to an array
$array[$file] = '';
}
}
}
// closing directory handle
closedir($handle);
}
// test it
$mytree = array();
myScanner('/var/www', $mytree);
print "<pre>";
print_r($mytree);
print "</pre>";
/$array是指向数组的指针
//$start是启动扫描的目录
函数myScanner($start,&$array){
//打开$start目录句柄
$handle=opendir($start);
//现在我们尝试读取目录内容
while(false!=($file=readdir($handle))){
//筛选。和..“文件夹”
如果($file!=“&&&$file!=”){
//用于测试此文件是否为目录的变量
$dirtest=$start.DIRECTORY\u SEPARATOR.$file;
//检查一下
if(is_dir($dirtest)){
//如果是目录,则再次运行该函数
//目录\此处使用分隔符,以避免将同名文件和目录混合使用
myScanner($dirtest,$array[$file.DIRECTORY_SEPARATOR]);
}否则{
//否则,我们只需将此文件添加到数组中
$array[$file]='';
}
}
}
//关闭目录句柄
closedir($handle);
}
//测试一下
$mytree=array();
myScanner('/var/www',$mytree);
打印“”;
打印(mytree);
打印“”;
非常感谢。。。但不想引起冒犯-这是一个相当密集的函数,我想要一个更温和的函数-也许有一些注释?再试一次,我修正了一点:)效果很好。我没有考虑使用参考资料。此行:myScanner($dirtest,$array[$file.DIRECTORY_SEPARATOR]);是一个让我感到困惑的问题——我没有看到关联键被添加到了哪里——直到我理解了引用启动变量。我今天学到了一些有价值的东西——谢谢!然后总是有SPL。。。急用
// $array is a pointer to your array
// $start is a directory to start the scan
function myScanner($start, &$array){
// opening $start directory handle
$handle = opendir($start);
// now we try to read the directory contents
while (false !== ($file = readdir($handle))) {
// filtering . and .. "folders"
if ($file != "." && $file != "..") {
// a variable to test if this file is a directory
$dirtest = $start . DIRECTORY_SEPARATOR . $file;
// check it
if (is_dir($dirtest)) {
// if it is the directory then run the function again
// DIRECTORY_SEPARATOR here to not mix files and directories with the same name
myScanner($dirtest, $array[$file . DIRECTORY_SEPARATOR]);
} else {
// else we just add this file to an array
$array[$file] = '';
}
}
}
// closing directory handle
closedir($handle);
}
// test it
$mytree = array();
myScanner('/var/www', $mytree);
print "<pre>";
print_r($mytree);
print "</pre>";