PHP Traversal tree Common method Summary, PHP all over the summary
This paper describes the common methods of PHP traversal tree. Share to everyone for your reference. Specific as follows:
First, recursive depth-first algorithm:
<?phpdefine (' DS ', directory_separator), function rec_list_files ($from = '. ') {if (!is_dir ($from)) {return array (); } $files = Array (); if ($dh = Opendir ($from)) {while (false!== ($file = Readdir ($DH))) {if ($file = = '. ' | | $file = = ' ... ') {continue; } $path = $from. Ds. $file; if (Is_file ($path)) {$files [] = $path; } $files = Array_merge ($files, Rec_list_files ($path)); } closedir ($DH); } return $files;} function profile ($func, $trydir) {$mem 1 = memory_get_usage (); Echo '-----------------------Test run for '. $func. ' () '; Flush (); $time _start = Microtime (true); $list = $func ($trydir); Print_r ($list); $time = Microtime (True)-$time _start; Echo ' finished: '. Count ($list). ' Files
'; $mem 2 = Memory_get_peak_usage (); printf ('Max memory for '. $func. ' ():%0.2f Kbytes Running time for '. $func. ' ():%0.f s
', ($mem 2-$mem 1)/1024.0, $time); return $list;} Profile (' rec_list_files ', ' D:\www\server ');? >
Second, recursive depth-first algorithm (using a stack to achieve)
<?phpdefine (' DS ', directory_separator), function deep_first_list_files ($from = '. ') {if (!is_dir ($from)) {return false; } $files = Array (); $dirs = Array ($from); while (NULL!== ($dir = Array_pop ($dirs))) {if ($dh = Opendir ($dir)) {while (false!== ($file = Readdir ($DH))) { if ($file = = '. ' | | $file = = ' ... ') {continue; } $path = $dir. Ds. $file; if (Is_dir ($path)) {$dirs [] = $path; } else {$files [] = $path; }} closedir ($DH); }} return $files;} function profile ($func, $trydir) {$mem 1 = memory_get_usage (); Echo '-----------------------Test run for '. $func. ' () '; Flush (); $time _start = Microtime (true); $list = $func ($trydir); Print_r ($list); $time = Microtime (True)-$time _start; Echo ' finished: '. Count ($list). ' Files
'; $mem 2 = Memory_get_peak_usage (); printf ('Max memory for '. $func. ' ():%0.2f Kbytes Running time for '. $func. ' ():%0.f s
', ($mem 2-$mem 1)/1024.0, $time); return $list;} Profile (' deep_first_list_files ', ' D:\www\server ');? >
Third, non-recursive breadth-first algorithm (using a queue to achieve)
<?phpdefine (' DS ', directory_separator), function breadth_first_files ($from = '. ') {$queue = Array (RTrim ($from, DS). DS);//normalize all paths $files = Array (); while ($base = Array_shift ($queue)) {if ($handle = Opendir ($base))} {while (($child = Readdir ($handle))!== FAL SE) {if ($child = = '. ' | | $child = = ' ... ') {continue; } if (Is_dir ($base. $child)) {$combined _path = $base. $child. DS; Array_push ($queue, $combined _path); } else {$files [] = $base. $child; }} closedir ($handle); }//Else unable to open directory = NEXT child} return $files; End of tree, file not found}function profile ($func, $trydir) {$mem 1 = memory_get_usage (); Echo '-----------------------Test run for '. $func. ' () '; Flush (); $time _start = Microtime (true); $list = $func ($trydir); Print_r ($list); $time = Microtime (True)-$time _start; Echo ' finished: '. Count ($list). ' Files
'; $mem 2 = Memory_get_peak_usage (); printf ('Max memory for '. $func. ' ():%0.2f Kbytes Running time for '. $func. ' ():%0.f s
', ($mem 2-$mem 1)/1024.0, $time); return $list;} Profile (' breadth_first_files ', ' D:\www\server ');? >
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1019074.html www.bkjia.com true http://www.bkjia.com/PHPjc/1019074.html techarticle PHP Traversal Tree Common method Summary, PHP all the examples of this article about the PHP traversal tree common methods. Share to everyone for your reference. Specific as follows: first, recursion depth first calculation ...