[PHP]利用PHP的引用生成树的结构

PHP中的引用就是两个变量指向了同一个地方,只要在变量前面增加了&符号,它就变成了一个引用

$a='aaa';
$b=&$a;
$c=&$b;
xdebug_debug_zval('a','b','c');

输出结果是:

a: (refcount=3, is_ref=1)='aaa'

b: (refcount=3, is_ref=1)='aaa'

c: (refcount=3, is_ref=1)='aaa'

“aaa”有了三个引用 , 并且是is_ref是引用类型,那也就意味着不管是我修改$b ,还是修改$c , "aaa"这个都会被改变

可以根据上面这个原理来把一个数据库存储的带pid的逐行数据,变成一个多层级的树状结构

$data=array(
    array("id"=>2,"pid"=>1),
    array("id"=>3,"pid"=>1),
    array("id"=>4,"pid"=>2),
    array("id"=>5,"pid"=>2),
    array("id"=>6,"pid"=>3),
    array("id"=>7,"pid"=>3),
    array("id"=>1,"pid"=>0),
);
$refer=array();//存储主键与数组单元的引用关系
//遍历
foreach($data as $k=>$v){
    $refer[$v['id']]=&$data[$k];//为每个数组成员建立对应关系
}
//遍历2
foreach($data as $k=>$v){
        $parent=&$refer[$v['pid']];//获取父分类的引用
        $parent['child'][]=&$data[$k];//在父分类的children中再添加一个引用成员
}
print_r($data);

利用了一个$refer数组,时间复杂度是O(n) , 只需要单层循环,直接通过引用修改$data原数据,生成一个树状结构

Array
(
    [0] => Array
        (
            [id] => 2
            [pid] => 1
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [pid] => 2
                        )

                    [1] => Array
                        (
                            [id] => 5
                            [pid] => 2
                        )

                )

        )

    [1] => Array
        (
            [id] => 3
            [pid] => 1
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [pid] => 3
                        )

                    [1] => Array
                        (
                            [id] => 7
                            [pid] => 3
                        )

                )

        )

    [2] => Array
        (
            [id] => 4
            [pid] => 2
        )

    [3] => Array
        (
            [id] => 5
            [pid] => 2
        )

    [4] => Array
        (
            [id] => 6
            [pid] => 3
        )

    [5] => Array
        (
            [id] => 7
            [pid] => 3
        )

    [6] => Array
        (
            [id] => 1
            [pid] => 0
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [pid] => 1
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [pid] => 2
                                        )

                                    [1] => Array
                                        (
                                            [id] => 5
                                            [pid] => 2
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [pid] => 1
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [pid] => 3
                                        )

                                    [1] => Array
                                        (
                                            [id] => 7
                                            [pid] => 3
                                        )

                                )

                        )

                )

        )

)

 

$a='aaa';
$b=&$a;
$c=&$b;
xdebug_debug_zval('a','b','c');
posted @ 2020-02-29 17:13  唯一客服系统开发笔记  阅读(588)  评论(0编辑  收藏  举报