大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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

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


网站标题:PHP中怎么引用生成树结构
新闻来源:http://dzwzjz.com/article/iidiic.html
在线咨询
服务热线
服务热线:028-86922220
TOP