nested set creat sub tree

70 views Asked by At

I have tree created with nested set which looks like:

user1
...user2
...user3
......user4
......user5
...user6
user7
...user8

DataBase Table schemat:

------------------------------------------
|usr_id |usr_name | usr_left | usr_right |
------------------------------------------

And i have array with ID of users to get but i have to keep structure of tree if i get any of user and his parent for example

if I want to get user1,user2 and user7 i want result will be:

user1
...user2
user7

if i want to get user3 and user6 and for example user8 result should be

user3
user6
user7

because user 7 and user 3 or 6 has not the same parents

but if i want to get tree with user1 and user4 (level 0 and level2) i want to get

user1
...[empty_indent-block]
......user4

so i have to keep structure with skip users which aren't in array.

I think about get all users from my array and group it by usr_left && usr_right but i don't know how can I do it in a simple way

1

There are 1 answers

1
Krzyś Iu On

So i have array with users which represents structures of them and array looks like:

array { 
    [0] => array{ 
               'id' => ID_OF_USER,
               'name' => NAME_OF_USER,
               'left' => LEFT_INDEX,
               'right'=> RIGHT_INDEX
           } 
    .
    .
    .
 }

and i have to create tree of this array so if id of element is beetwen left and right of other element - element2 then element is child of element2

next i have to convert my tree to one dimension array with level of indents