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
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包