How to create a multidimensional array from hierarchically stored SQL data using the adjacency list method?

Hierarchical data from SQL

Abutment List Model

In my model, I have a series of objects, each of which is stored with their parent identifier. I use the Adjacency list model as a hierarchy method.

All examples of the Adjacency list are simply displayed there and then. None of them are trying to create a multidimensional array from a result set.

---------------
| id | parent |
---------------
| 1  | NULL   |
| 2  | 1      |
| 3  | 1      |
| 4  | 2      |
| 5  | 2      |
| 6  | 5      |
---------------

An object

I created an array variable in my class called "children" and I want to add a child object every time I find a child from a db request.

. ? , .

, ?

PHP, ?

+3
2

; ad-hoc-, node , . PHP, :

{
    object => $row1,
    children => [
        {
            object => $row2,
            children => [ ... ],
        }, {
            object => $row3,
            children => [],
        }
    ]
}
+1

Source: https://habr.com/ru/post/1715109/


All Articles