php常见递归实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/best_go_to_success/article/details/53080608

今天我做了一个递归

首先,我们来看一下数据库表的设计

这个就是我们的数据库了

接下来我们首先获取数据

<?php
header('content-type:text/html;charset=utf-8');
$pdo=new PDO('mysql:host=127.0.0.1;dbname=test','root','root');
$pdo->exec('set names utf8');
$sql='select * from test';
$res=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);

实现一个简单的树状递归

function tree($data,$pid=0,$level=0) {
    static $arr;
    foreach($data as $k=>$v) {
        if($v['pid'] == $pid) {
            $v['level']=$level;
            $arr[]=$v;
            tree($data,$v['id'],$level+1);
        }
    }
    return $arr;
}

$res1=tree($res);

打印我们的数据print_r($res1);

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 交通工具
            [pid] => 0
            [level] => 0
        )

    [1] => Array
        (
            [id] => 2
            [name] => 汽车
            [pid] => 1
            [level] => 1
        )

    [2] => Array
        (
            [id] => 3
            [name] => 宝马
            [pid] => 2
            [level] => 2
        )

    [3] => Array
        (
            [id] => 8
            [name] => bm1
            [pid] => 3
            [level] => 3
        )

    [4] => Array
        (
            [id] => 7
            [name] => 火车
            [pid] => 1
            [level] => 1
        )

    [5] => Array
        (
            [id] => 4
            [name] => 食品
            [pid] => 0
            [level] => 0
        )

    [6] => Array
        (
            [id] => 5
            [name] => 水果
            [pid] => 4
            [level] => 1
        )

    [7] => Array
        (
            [id] => 6
            [name] => 苹果
            [pid] => 5
            [level] => 2
        )

)

紧接着我们来实现一个子孙递归

function son($data,$pid=0) {
    $arr=array();
    foreach($data as $k=>$v) {
        if($v['pid'] == $pid) {
            $v['son']=son($data,$v['id']);
            $arr[]=$v;
        }
    }
    return $arr;
}

以上就是我们的一个子孙递归,我们试着调用一下

$res2=son($res);

print_r($res2);

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 交通工具
            [pid] => 0
            [son] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [name] => 汽车
                            [pid] => 1
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [name] => 宝马
                                            [pid] => 2
                                            [son] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 8
                                                            [name] => bm1
                                                            [pid] => 3
                                                            [son] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 7
                            [name] => 火车
                            [pid] => 1
                            [son] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 4
            [name] => 食品
            [pid] => 0
            [son] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [name] => 水果
                            [pid] => 4
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [name] => 苹果
                                            [pid] => 5
                                            [son] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页