温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP中怎么引用生成树结构

发布时间:2021-06-22 15:40:50 来源:亿速云 阅读:171 作者:Leah 栏目:大数据

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');

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI