开发者

how can recursion function return accumulative array(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 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,wh开发者_StackOverflow社区en 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"


This is a paraphrased pseudo-code version showing the recursive merging logic:

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

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 let file_list go through it, merging the results into your current $result array (because file_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 be file_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.


Best way to do what you're trying to do: let PHP recurse for you.

$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 );
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜