递归函数如何返回累计数组(array_merge)(php)
列表.数据文件如下:递归函数如何返回累计数组(array_merge)(php),php,recursion,array-merge,Php,Recursion,Array Merge,列表.数据文件如下: function file_list($path){ $final_result=array(); if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (is_dir($path."/".
function file_list($path){
$final_result=array();
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($path."/".$file)) {
//echo $path."/".$file."\n";//directory
return file_list($path."/".$file);
} else {
//echo $path."/".$file."\n"; //all file
if(stripos($path."/".$file,"playlist.data"))
{
//echo $path."/".$file."\n"; //file contains "list.data"
$content = file_get_contents($path."/".$file);
preg_match_all("/([0-9]*).txt,0/",$content, $matches);
$result=array_unique($matches[1]);
$final_result=array_merge($final_result,$result);
$final_result=array_unique($final_result);
sort($final_result);
return($final_result);
}
}
}
}
}
}
print_r(file_list($argv[1]));
1.txt
3.txt
2.txt
4.txt
另一个列表.data文件如下:
function file_list($path){
$final_result=array();
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($path."/".$file)) {
//echo $path."/".$file."\n";//directory
return file_list($path."/".$file);
} else {
//echo $path."/".$file."\n"; //all file
if(stripos($path."/".$file,"playlist.data"))
{
//echo $path."/".$file."\n"; //file contains "list.data"
$content = file_get_contents($path."/".$file);
preg_match_all("/([0-9]*).txt,0/",$content, $matches);
$result=array_unique($matches[1]);
$final_result=array_merge($final_result,$result);
$final_result=array_unique($final_result);
sort($final_result);
return($final_result);
}
}
}
}
}
}
print_r(file_list($argv[1]));
1.txt
3.txt
2.txt
4.txt
所以结果应该是这样的数组:
function file_list($path){
$final_result=array();
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($path."/".$file)) {
//echo $path."/".$file."\n";//directory
return file_list($path."/".$file);
} else {
//echo $path."/".$file."\n"; //all file
if(stripos($path."/".$file,"playlist.data"))
{
//echo $path."/".$file."\n"; //file contains "list.data"
$content = file_get_contents($path."/".$file);
preg_match_all("/([0-9]*).txt,0/",$content, $matches);
$result=array_unique($matches[1]);
$final_result=array_merge($final_result,$result);
$final_result=array_unique($final_result);
sort($final_result);
return($final_result);
}
}
}
}
}
}
print_r(file_list($argv[1]));
1.txt
3.txt
2.txt
4.txt
我的目标是递归搜索文件系统的指定目录,当我找到名为“list.data”的文件名时,我读取了这个文件,并使用正则表达式过滤和数组到$result
。由于我有不止一个“list.data”,我想将每个$result
合并到$final\u result
。但是这个脚本没有输出任何内容。
谁能告诉我文件列表
功能有什么问题吗
我正在CLI模式下执行此php脚本,如下所示:
php.exe script.php“d:/test/listdir”这是一个解释过的伪代码版本,显示递归合并逻辑:
Array(
[0]=>1
[1]=>2
[2]=>3
[3]=>4
)
函数总是返回一个数组,即使它是空的。浏览一个目录,将结果添加到$result
数组中。如果遇到子目录,您可以让文件列表
遍历它,将结果合并到当前的$result
数组中(因为文件列表
返回一个结果数组)。最后,在处理完目录中的所有条目后,将结果列表返回给调用者(可能是file\u list
本身)
如果您想尝试,请查看,这将允许您展平函数中的逻辑,使其非递归。这是一个解释过的伪代码版本,显示递归合并逻辑:
Array(
[0]=>1
[1]=>2
[2]=>3
[3]=>4
)
函数总是返回一个数组,即使它是空的。浏览一个目录,将结果添加到$result
数组中。如果遇到子目录,您可以让文件列表
遍历它,将结果合并到当前的$result
数组中(因为文件列表
返回一个结果数组)。最后,在处理完目录中的所有条目后,将结果列表返回给调用者(可能是file\u list
本身)
如果您想进行实验,请查看,这将使您能够平坦化函数中的逻辑,使其成为非递归的。完成您尝试的操作的最佳方法:让PHP为您递归
function file_list($path) {
$result = array();
foreach ($path as $file) {
if (is_dir($file)) {
$result = array_merge($result, file_list($file));
} else {
$result[] = ...
}
}
return $result;
}
做你想做的事情的最好方法:让PHP为你递归
function file_list($path) {
$result = array();
foreach ($path as $file) {
if (is_dir($file)) {
$result = array_merge($result, file_list($file));
} else {
$result[] = ...
}
}
return $result;
}
你需要把glob放在
$file
@cwallen周围,就像我说的:解释的伪代码
。我想展示逻辑,而不是细节。当然。我只是看到了代码块,我的大脑循环了几次,我想我需要<50个字符才能让你知道。你需要把glob放在$file
@cwallen周围,就像我说的:解释伪代码
。我想展示逻辑,而不是细节。当然。我只是看到了代码块,我的大脑循环了几次,我想我需要<50个字符才能让你知道。