dongleiqiao4906 2019-05-05 11:45
浏览 205
已采纳

从数组构建父子关系

We have a list of random categories which needs to arranged in a JSON output as per folowing rules - a. all parent categories should be sorted alphabetically b. each parent category should have an arry of its children sorted alphabatecally c. parent-child relationship can be multilevel

    function hierarchy($data){
        $arr=array();
        foreach($data as $row){
            $subarr=[];
            if($row['parent']==null){
                $subarr[$row['_id']]=$row;
                $childs=findcs($row,$data);
                print_r($childs);
            }
            array_push($arr,$subarr);
        }
        print_r($arr);
    }

    function findcs($row,$data){
        $allChilds=[];
        foreach($data as $item){
            if($item['_id']==$item['parent']){
                $child=[];
                $child[$item['_id']]=$item;
                array_push($allChilds,$child);
                findcs(item['_id'],$data);
            }
        }
        return $allChilds;
    }

$data=[
    [
        "name"=> "Travel",
        "parent"=> null,
        "_id"=> 1,
    ],
    [
        "name"=>"Air Travel",
        "parent"=> 1,
        "_id"=> 1212,
    ],
    [
        "name"=> "Hotel",
        "parent"=> 1,
        "_id"=>212,
    ],
    [
        "name"=> "Businss Exp",
        "parent"=> null,
        "_id"=> 2,
    ],
    [
        "name"=> "Taxes",
        "parent"=> 2,
        "_id"=> 34,
    ],
    [
        "name"=> "Local Tax",
        "parent"=> 34,
        "_id"=> 34111,
    ],
    [
        "name"=>"Licenses",
        "parent"=> 34,
        "_id"=> 111232,
    ],
    [
        "name"=> "Insurance",
        "parent"=> 1212,
        "_id"=>113412,
    ],
];
  • 写回答

1条回答 默认 最新

  • duanhai4046 2019-05-05 13:05
    关注

    This code will assume that a parent will appear before a child element. It adds anything without a parent id to the base of the output array ( $out ), otherwise it calls the recursive function addChild(). (Forgot about, so added) When adding each entry to the existing children, this uses usort() to sort the entries in alphabetical order.

    This looks at each node and checks if it is the parent node, if not it will also look at any children of this node as well (if any) and so on...

    function addChild ( $element, &$tree )   {
        foreach ( $tree as &$leaf )  {
            if ( $leaf['_id'] == $element['parent'] )   {
                $leaf['children'][] = $element;
                usort($leaf['children'], 
                    function($a, $b) { 
                        return strcmp($a["name"], $b["name"]); 
                    });
                break;
            }
            if ( isset($leaf['children']) ) {
                addChild($element, $leaf['children']);
            }
        }
    }
    $out = [];
    foreach ( $data as $element )   {
        if ( empty($element['parent']) )  {
            $out[] = $element;
        }
        else    {
            // Look for parent
            addChild($element, $out);
        }
    }
    

    Creates...

    Array
    (
        [0] => Array
            (
                [name] => Travel
                [parent] => 
                [_id] => 1
                [children] => Array
                    (
                        [0] => Array
                            (
                                [name] => Air Travel
                                [parent] => 1
                                [_id] => 1212
                                [children] => Array
                                    (
                                        [0] => Array
                                            (
                                                [name] => Insurance
                                                [parent] => 1212
                                                [_id] => 113412
                                            )
    
                                    )
    
                            )
    
                        [1] => Array
                            (
                                [name] => Hotel
                                [parent] => 1
                                [_id] => 212
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [name] => Businss Exp
                [parent] => 
                [_id] => 2
                [children] => Array
                    (
                        [0] => Array
                            (
                                [name] => Taxes
                                [parent] => 2
                                [_id] => 34
                                [children] => Array
                                    (
                                        [0] => Array
                                            (
                                                [name] => Licenses
                                                [parent] => 34
                                                [_id] => 111232
                                            )
    
                                        [1] => Array
                                            (
                                                [name] => Local Tax
                                                [parent] => 34
                                                [_id] => 34111
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多