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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失