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

从数组构建父子关系

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
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题