递归函数如何返回累积数组(array_merge)(php)
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]));
list.data 文件如下:
1.txt
3.txt
另一个 list.data 文件如下:
2.txt
4.txt
所以结果应该是这样的数组:
Array(
[0]=>1
[1]=>2
[2]=>3
[3]=>4
)
我的目标是搜索文件系统的指定目录递归地,当我找到名为“list.data”的文件名时,我读取该文件并使用正则表达式进行过滤并数组到$result
。因为我有多个“list.data”,所以我想将每个 $result
合并到 $final_result
。但这个脚本什么也没输出。 谁能告诉我 file_list
函数出了什么问题吗?
我在 CLI 模式下执行这个 php 脚本,如下所示: php.exe script.php "d:/test/listdir"
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]));
list.data file like this:
1.txt
3.txt
another list.data file like this:
2.txt
4.txt
so the result should be array like this:
Array(
[0]=>1
[1]=>2
[2]=>3
[3]=>4
)
I was aim to search the specified directory of the file system recursively,when I found file name called "list.data", I read this file and using regex to filter and array to $result
. since I have more than one "list.data", I wannted to merge every $result
to $final_result
. but this script output nothing.
can anybody tell me does something wrong with file_list
function.
i am execute this php script in CLI mode like this:
php.exe script.php "d:/test/listdir"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个解释的伪代码版本,显示了递归合并逻辑:
该函数始终返回一个数组,即使它是空的。浏览目录,将结果添加到
$result
数组中。如果遇到子目录,则让file_list
遍历它,将结果合并到当前的$result
数组中(因为file_list
返回一个数组结果)。最后,在处理完目录中的所有条目后,将结果列表返回给调用者(可能是file_list
本身)。如果您想尝试一下,请查看
RecursiveDirectoryIterator
,它可以让您扁平化函数中的逻辑,使其非递归。This is a paraphrased pseudo-code version showing the recursive merging logic:
The function always returns an array, even if it's empty. Going through a directory, you add your results into the
$result
array. If you encounter a subdirectory, you letfile_list
go through it, merging the results into your current$result
array (becausefile_list
returns an array of results). Finally, after all entries in the directory have been handled, you return the list of results to the caller (which may befile_list
itself).If you feel like experimenting, look into the
RecursiveDirectoryIterator
, which would allow you to flatten the logic in your function, making it non-recursive.完成您想要做的事情的最佳方法:让 PHP 为您递归。
Best way to do what you're trying to do: let PHP recurse for you.