【问题标题】:how can recursion function return accumulative array(array_merge) (php)递归函数如何返回累积数组(array_merge)(php)
【发布时间】:2011-07-22 05:48:21
【问题描述】:
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"

【问题讨论】:

    标签: php recursion array-merge


    【解决方案1】:

    这是一个解释了递归合并逻辑的伪代码版本:

    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;
    }
    

    该函数总是返回一个数组,即使它是空的。通过一个目录,您将结果添加到 $result 数组中。如果遇到子目录,则让file_list 遍历它,将结果合并到当前的$result 数组中(因为file_list 返回结果数组)。最后,在处理完目录中的所有条目后,将结果列表返回给调用者(可能是 file_list 本身)。

    如果您想尝试一下,请查看RecursiveDirectoryIterator,它可以让您扁平化函数中的逻辑,使其成为非递归的。

    【讨论】:

    • 你需要把 glob 放在$file
    • @cwallen 正如我所说:paraphrased pseudo-code。我想展示逻辑,而不是细节。
    • 当然。我刚刚看到代码块,我的大脑循环了几次,并认为我需要全部
    【解决方案2】:

    做你想做的事的最佳方式:让 PHP 为你递归。

    $iter = new RecursiveIteratorIterator( 
               new RecursiveDirectoryIterator( 
                    $path, FilesystemIterator::CURRENT_AS_FILEINFO ) );
    
    $out = array();
    foreach( $iter as $file )
    {
        if( $file->getBasename() == 'list.data' )
        {
            $listdata = file_get_contents( $file->getPathname() );
            // do something with list.data
            $out = array_merge( $listDataOutput, $out );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-18
      • 2015-04-27
      • 2021-08-14
      • 2020-02-20
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      相关资源
      最近更新 更多