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