Php recursive instance

Source: Internet
Author: User
: This article mainly introduces php recursive instances. if you are interested in PHP tutorials, you can refer to them. Recursive algorithms are widely used in programming languages. A process or function has a method that calls itself directly or indirectly in its definition or description, it usually converts a large and complex problem into a small problem similar to the original problem to solve it, the recursive strategy can describe the repeated computing required for the problem-solving process with only a small number of programs, greatly reducing the amount of code in the program.

Here are four recursive instances:

 $ V) {if (is_array ($ v) {$ sum + = funtmp ($ v, $ sum) ;}} return $ sum ;} return funtmp ($ arr);} // $ arr = array (, array (, 3, array ))); // echo conarr ($ arr ).'
'; // Echo count ($ arr, 1 ); /*** recursively count the number of sub-files and the number of folders in a specified directory * @ param $ dirname string directory path * @ return array | boolean returns an array containing the number of sub-files and the number of folders, returns false */function condir ($ dirname, $ data = array ('dirnum' => 0, 'filenum' => 0) {if (! Is_dir ($ dirname) {return false;} $ dir = opendir ($ dirname); // open the handle readdir ($ dir); // read the vertex readdir ($ dir ); // read point while ($ filename = readdir ($ dir) {$ newfile = $ dirname. '/'. $ filename; // splice the 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 a file or folder * @ param string $ dirname file path * @ return boolean success return true, 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 '); // Infinitely Polar classification sorting. the parent class is followed by the 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 introduces php recursive instances, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.