PHP Recursive instance

Source: Internet
Author: User
Recursion as an algorithm is widely used in programming language. A procedure or function has a method of directly or indirectly invoking itself in its definition or description, which usually transforms a large and complex problem layer into a smaller problem similar to the original problem, and the recursive strategy can describe the repeated computations needed in the process of solving the problems by a small number of programs. Greatly reduces the code volume of the program.

Here are 4 examples of recursion:

 $V) {if (Is_array ($v)) {$sum + = funtmp ($v, $sum);        }} return $sum;    } return Funtmp ($arr);    }//$arr = Array (1,2,3,4,array (1,2,3,array (0,3))); echo Conarr ($arr). '
';    //echo Count ($arr, 1);    /**    * Recursive statistics the number of sub-files in the specified directory and the number of folders      * @param $dirname string directory path     * @return Array|boolean Returns an array that contains the number of child files and the number of folders, failed to return false     */    function Condir ($dirname, $data = Array (' Dirnum ' =>0, ' FileNum ' =>0)) {         if (!is_dir ($dirname)) {             return false;        }         $dir = Opendir ($dirname); Open handle         readdir ($dir);//Read points         readdir ($dir) ;//Read points         while ($filename = Readdir ($dir)) {              $newfile = $dirname. ' /'. $filename;//splicing sub-file name             if (Is_dir ($newfile)) {                 $data [' Dirnum ']++;                 $data [ ' Dirnum ']+=condir ($newfile) [' Dirnum '];                 $data [' FileNum ']+=condir ($newfile) [' FileNum '];            }else{                  $data [' FileNum ']++;             }        }         return $data;    }    //$a =  condir (' C:\wamp\www\test ');    //var _dump ($a);    /**    * Delete file or folder     * @param string $dirname file path  & nbsp  * @return Boolean Delete successfully returns TRUE, Failure returns false    */    function Deldir ($dirname) {         if (!file_exists ($dirname)) {return false;}         if ($dir = Opendir ($dirname)) {            while ($filename = Readdir ($dir)) {                 if ($filename! = "." && $filename! = ' ... ') {                     $subFile = $dirname. ' /'. $filename;                    if (is_ Dir ($subFile)) {                         deldir ($subFile);                     }                     if (Is_file ($subFile)) {                        unlink ($subFile);                     }                 }            }                 closedir ($dir);             rmdir ($dirname);        }         if (!file_exists ($dirname)) {            return true;         }else{            return false;         }    }    //echo deldir (' C:\wamp\www\test ');     //infinite pole sorting, parent class followed by subclass     function GetList ($cate, $pid =0, $html = "------", $i =0) {         $i ++;        $list = Array ();        foreach ($cate as $val) {             if ($val [' pid ']== $pid) {                 $val [' HTML ']=str_repeat ($html, $i-1);                 $list []= $val;                 $list = Array_merge ($list, GetList ($cate, $val [' id '], $html, $i));            }       }         return $list;   }?>


The above describes the PHP recursive instance, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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