获取mysql以获取分层数据结构

时间:2016-12-22 21:09:24

标签: php sql database hierarchical-data

我有这个数据库结构:

USERS
-----------------------------
| id | parent | name | points
-----------------------------

我需要从这个结构中获得多个嵌套(分层)数组。

例如,从这些数据:

USERS
------------------------------
| id | parent | name | points
------------------------------
| 1  | null   | A    | 20
| 2  | 1      | B    | 10
| 3  | 1      | C    | 30
| 4  | 3      | D    | 40
| 5  | 2      | E    | 50
------------------------------

如何获取以下php数组:

[
  "1" => [
    "points" => 20,
    "childs" => [
      "2" => [
        "points" => 10,
        "childs" => [
          "5" => [
            "points" => 50,
            "childs" => null
          ]
        ]
      ],
      "3" => [
        "points" => 30,
        "childs" => [
          "4" => [
            "points" => 40,
            "childs" => null
          ]
        ]
      ]
    ]
  ]
]

谢谢!

1 个答案:

答案 0 :(得分:1)

这是一个无限深度的工作示例:

 //Obtain this from database
 $datas = [
    [
        "id" => 1,
        "parent" => null,
        "name"  => "A",
        "points" => 20
    ],
    [
        "id" => 2,
        "parent" => 1,
        "name"  => "B",
        "points" => 10
    ],
    [
        "id" => 3,
        "parent" => 1,
        "name"  => "C",
        "points" => 30
    ],
    [
        "id" => 4,
        "parent" => 3,
        "name"  => "D",
        "points" => 40
    ],
    [
        "id" => 5,
        "parent" => 2,
        "name"  => "E",
        "points" => 50
    ]   
 ];

 $ordered = [];

 for($i=0; $i<count($datas); $i++)
 {
    $data = &$datas[$i];
    $id = $data["id"];
    $data["childs"] = [];
    $ordered[$id] = &$data; 
 }

 $result = [];
 for($i=0; $i<count($datas); $i++)
 {
    $data = &$datas[$i];
    $id = $data["id"];
    $parent = $data["parent"];

    //unset not needed properties
    unset($data["id"]);
    unset($data["parent"]);                 

    if($parent){
        $ordered[$parent]["childs"][$id] = &$data;
    } else {
        $result[$id] = &$data;
    }
 }

结果是:

print_r($result);

Array
(
    [1] => Array
        (
            [name] => A
            [points] => 20
            [childs] => Array
                (
                    [2] => Array
                        (
                            [name] => B
                            [points] => 10
                            [childs] => Array
                                (
                                    [5] => Array
                                        (
                                            [name] => E
                                            [points] => 50
                                            [childs] => Array
                                                (
                                                )    
                                        )    
                                )    
                        )    

                    [3] => Array
                        (
                            [name] => C
                            [points] => 30
                            [childs] => Array
                                (
                                    [4] => Array
                                        (
                                            [name] => D
                                            [points] => 40
                                            [childs] => Array
                                                (
                                                )    
                                        )    
                                )    
                        )    
                )    
        )    
)

ordered包含由id“排序”的数据数组(因此您可以轻松搜索项目) result包含数据层次结构。

希望有所帮助