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 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题