php recursive example

WBOY
Release: 2016-07-29 09:14:56
Original
1020 people have browsed it

Recursion as an algorithm is widely used in programming languages. A process or function has a method of directly or indirectly calling itself in its definition or description. It usually transforms a large and complex problem into a smaller problem similar to the original problem to solve. The recursive strategy only A small number of programs are needed to describe the multiple repeated calculations required in the problem-solving process, which greatly reduces the amount of program code.

Here are 4 examples of recursion:

<?php
&#160;&#160; &#160;
&#160;&#160; &#160;/*
&#160;&#160; &#160;&#160;&#160; &#160;递归的实例
&#160;&#160; &#160;&#160;&#160; &#160;1、统计数组元素个数
&#160;&#160; &#160;&#160;   php也可以直接使用count($arr,1) 来计算多维数组的元素的个数
&#160;&#160; &#160;&#160;&#160; &#160;2、统计文件和文件夹数量
&#160;&#160; &#160;&#160;&#160; &#160;3、删除文件或文件夹
&#160;&#160; &#160;&#160;&#160; &#160;用到的系统函数:file_exists()检查目录或者文件是否存在 unlink()删除文件 rmdir()删除目录
&#160;&#160; &#160;&#160;&#160; &#160;4、无限极分类的排序
&#160;&#160; &#160;*/

&#160;&#160; &#160;/**
&#160;&#160; &#160;* 递归共计数组元素个数
&#160;&#160; &#160;* @param array $arr 统计的数组
&#160;&#160; &#160;* @return boolean|int&#160; 如果失败返回false,成功返回数组的元素个数
&#160;&#160; &#160;*/
&#160;&#160; &#160;function conarr($arr){
&#160;&#160; &#160;&#160;&#160; &#160;function funtmp($arr,$sum=0){
&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;if(is_array($arr)){
&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;$sum=count($arr);
&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;}else{
&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;return count($arr);
&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;}
&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;foreach($arr as $k=> $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).'<br/>';
    // echo count($arr,1);

    /**
    * 递归统计指定目录的子文件的个数和文件夹个数
    * @param $dirname string 目录路径
    * @return array|boolean 返回包含子文件个数和文件夹个数的数组,失败返回false
    */
    function condir($dirname,$data = array('dirnum'=>0,'filenum'=>0)){
        if(!is_dir($dirname)){
            return false;
        }
        $dir = opendir($dirname); //打开句柄
        readdir($dir);//读取点
        readdir($dir);//读取点
        while($filename = readdir($dir)){
            $newfile = $dirname.'/'.$filename;//拼接子文件名
            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);

    /**
    * 删除文件或者文件夹
    * @param string $dirname 文件路径
    * @return boolean 删除成功返回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');


    //无限极分类排序,父类后跟子类
    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;
    }
?>
Copy after login


The above has introduced PHP recursion examples, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template