• php 数组转化成为树形结构


    <?php
    $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
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
  • 相关阅读:
    645. Set Mismatch
    400. Nth Digit
    633. Sum of Square Numbers
    507. Perfect Number
    453. Minimum Moves to Equal Array Elements
    441. Arranging Coins
    Girls and Boys
    二分图
    Gap
    SZU-A22
  • 原文地址:https://www.cnblogs.com/kevin-yang123/p/14154818.html
Copyright © 2020-2023  润新知