按键将平面数组分组为多维数组,如果我们不知道有多少级别.PHP

gdf*_*dfg 5 php arrays sorting algorithm multidimensional-array

我有这个数组:

$arr = [
["id"=>20,
    "name"=>"a",
    "parent"=>28,
],
["id"=>21,
    "name"=>"a-child",
    "parent"=>20,
],
["id"=>27,
    "name"=>"a-child-b",
    "parent"=>20,
],
["id"=>28,
   "name"=>"A parent",
   "parent"=>0,
],
["id"=>12,
    "name"=>"no parent",
    "parent"=>0,
]];
Run Code Online (Sandbox Code Playgroud)

我想要的是根据parent键对其进行分组,其中parent = id && parent > 0或者id是该元素的父元素,如果父键大于零,则元素具有父元素.

在上面的数组id=12中没有父元素,id=20有子元素21, 27,它是一个子元素id=28.

我做了什么 :

    public function sort($arr){

    $result = [];

    // Get child
    foreach($arr as $key => $row) {
        if($row['parent'] > 0) {
            $result[$row->parent][] = ['id' => $row['id'], 'name' => $row['name']];
            unset($arr[$key]);
        }
    }

    // Get parent and append child
    foreach($arr as $key => $row) {
        $result[$row['id']] = ['name' => $row['name'],
                              'child' => $result[$row['id']]];

    }
    return $result;
}
Run Code Online (Sandbox Code Playgroud)

那个问题是,这只适用于1级孩子parent => child array().

我想要的是一个获取参数的方法(在数组之上),我不知道,我将拥有多少级别的嵌套并parent按键数组返回:

$arr = [
["id"=>28,
 "name"=>"A parent",
 "parent"=>0,
    'child' => [
        ["id"=>20,
             "name"=>"a",
             "parent"=>28,
             'child' => [
                 ["id"=>21,
                  "name"=>"a-child",
                  "parent"=>20,
                 ],
                 ["id"=>27,
                  "name"=>"a-child-b",
                  "parent"=>20,
                 ]
             ]
        ]
    ]
],
["id"=>12,
 "name"=>"no parent",
 "parent"=>0,
]];
Run Code Online (Sandbox Code Playgroud)

viv*_*_23 2

<?php

define('ROOT_PARENT',0);

function getHierarchy($records){
    $hierarchy = [];

    /*
        let's assume everybody is going to be a parent
    */

    foreach($records as $each_record){
        $each_record['child'] = [];
        $hierarchy[$each_record['id']] = $each_record;
    }

    /*
       Now add child to parent's key in $hierarchy in the 'child' key. 
       The & is important since there may be future childs for current child. So pass by reference is needed
    */
    foreach($records as $each_record){
        $hierarchy[$each_record['parent']]['child'][] = &$hierarchy[$each_record['id']];
    }

    /* 
        here I unset every key which wasn't at root level,i.e is 0(top) level
    */
    foreach($hierarchy as $parent => $its_data){
        if($parent != ROOT_PARENT){
            unset($hierarchy[$parent]);
        }
    }

    return isset($hierarchy[ROOT_PARENT],$hierarchy[ROOT_PARENT]['child']) ? $hierarchy[ROOT_PARENT]['child'] : [];
}

$records = [
            [
                "id" => 20,
                "name" => "a",
                "parent" => 28,
            ],
            [
                "id" => 21,
                "name" => "a-child",
                "parent" => 20,
            ],
            [
                "id" => 27,
                "name" => "a-child-b",
                "parent" => 20,
            ],
            [
                "id" => 28,
               "name" => "A parent",
               "parent" => 0,
            ],
            [
                "id" => 12,
                "name" => "no parent",
                "parent" => 0,
            ]
    ];


echo "<pre>";
print_r(getHierarchy($records));
Run Code Online (Sandbox Code Playgroud)

输出:

Array
(
    [0] => Array
        (
            [id] => 28
            [name] => A parent
            [parent] => 0
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 20
                            [name] => a
                            [parent] => 28
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 21
                                            [name] => a-child
                                            [parent] => 20
                                            [child] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 27
                                            [name] => a-child-b
                                            [parent] => 20
                                            [child] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 12
            [name] => no parent
            [parent] => 0
            [child] => Array
                (
                )

        )

)
Run Code Online (Sandbox Code Playgroud)

首先,我们认为每个人都可以成为父母。然后,在它的父项child密钥中,我们不断添加它的子项。我们通过key引用传递,因为可能会有未来的孩子。最后,unset()来自层次结构中非根父级的每个人。最后,您就得到了最终的层次结构。