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

这个就是我们的数据库了

接下来我们首先获取数据

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                                                (                                                )                                        )                                )                        )                )        ))