PHP traversal tree Common methods Summary, PHP _php tutorial

Source: Internet
Author: User

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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.