Все объекты без parent_id должны быть корневыми объектами в новом массиве. Вставлен в массив детей правильного объекта: < /p>
Так что это мой оригинальный массив: < /strong> < /p>
Код: Выделить всё
array
0 =>
object(Node)[528]
protected 'id' => int 1
protected 'parent_id' => null
1 =>
object(Node)[529]
protected 'id' => int 2
protected 'parent_id' => null
2 =>
object(Node)[530]
protected 'id' => int 3
protected 'parent_id' => 1
3 =>
object(Node)[531]
protected 'id' => int 4
protected 'parent_id' => 1
4 =>
object(Node)[532]
protected 'id' => int 5
protected 'parent_id' => 4
5 =>
object(Node)[533]
protected 'id' => int 6
protected 'parent_id' => 4
Код: Выделить всё
$nodes = array(
array(
'id' => 1,
'parent_id' => null,
'children' => array(
array(
'id' => 3,
'parent_id' => 1,
'children' => null
),
array(
'id' => 4,
'parent_id' => 1,
'children' => array(
array(
'id' => 5,
'parent_id' => 4
),
array(
'id' => 6,
'parent_id' => 5
),
)
),
),
),
array(
'id' => 2,
'parent_id' => null,
'children' => null
),
);
Подробнее здесь: https://stackoverflow.com/questions/381 ... child-rela