I am having trouble in converting a flat array, e.g. from a DB query, into a tree structure. I have something like this:
[
[
id => 11,
path => '/11',
label => 'AAA'
],
[
id => 12,
path => '/12',
label => 'BBB'
],
[
id => 21,
path => '/12/21',
label => 'BBBB'
],
[
id => 21,
path => '/12/22',
label => 'CCCCC'
],
]
path points to the hierarchical position inside the tree, defined by the id's. So in the end I have to get something like this:
$tree=[
[
'id' => '11',
'label' => 'AAA'
],
[
'id' => '12',
'label' => 'BBB',
'children' => [
[
'id' => '21',
'label' => 'BBBB'
],
[
'id' => '22',
'label' => 'CCCCC'
]
]
]
];
The depth can be infinite. I would appreciate any solution. Thank you :-)
'/x/y'you can guarantee that you already have accessed the node with id'/x'?