PHP Create a nested array from MySQL data.

I have some data stored in a table, for example:

id  parent_id  name
1   0          Entry 1
2   0          Entry 2
3   0          Entry 3
4   1          Child of entry 1

I want to turn it into a nested array as follows:

array(
    array(
        'id' => 1,
        'parent_id' => 0,
        'name' => 'Entry 1',
        'children' => array(...)
    ),
    ...
);

Ideally, it should support an infinite amount of nesting (children with children). Is my table set up to support this, if so, how can I generate such an array using the data in the table? If not, how do I set up the table?

+3
source share
2 answers

There is a very good description of hierarchical data management in mysql: hierarchical data management Here is another good example of building nested arrays: building nested arrays

. mutch, , , .

, .

+5

"" SQL, , . , , , .

0

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


All Articles