How does php calculate the array structure of the organization chart in the output data table

my database table user_tree looks like this. This is a left-right value tree, where Level represents the depth of the tree, Lft is the left value, and Rgt is the right value.

< table > < tbody > < tr class= "firstRow" > < th width= "147" > Userid < / th > < th width= "147" > Name < / th > < th width= "147" > Lft < / th > < th width= "147" > Rgt < / th > < th colspan= "1" rowspan= "1" width= "147" > Level < / th > < / tr > < tr > < td width= "147" > 1001 < / td > < td width= "147" > Wang < / td > < td width= "147" > one < / td > < td width= "147" > sixteen < / td > < td colspan= "1" rowspan= "1" width= "147" > one < / td > < / tr > < tr > < td width= "147" > 1002 < / td > < td width= "147" > Li < / td > < td width= "147" > two < / td > < td width= "147" > seven < / td > < td colspan= "1" rowspan= "1" width= "147" > two < / td > < / tr > < tr > < td width= "147" > 1003 < / td > < td width= "147" > Wlm < / td > < td width= "147" > eight < / td > < td width= "147" > fifteen < / td > < td colspan= "1" rowspan= "1" width= "147" > two < / td > < / tr > < tr > < td width= "147" > 1005 < / td > < td width= "147" > Bander < / td > < td width= "147" > three < / td > < td width= "147" > four < / td > < td colspan= "1" rowspan= "1" width= "147" > three < / td > < / tr > < tr > < td width= "147" > 1008 < / td > < td width= "147" > Qipl < / td > < td width= "147" > five < / td > < td width= "147" > six < / td > < td colspan= "1" rowspan= "1" width= "147" > three < / td > < / tr > < tr > < td width= "147" > 1007 < / td > < td width= "147" > Buliao < / td > < td width= "147" > nine < / td > < td width= "147" > twelve < / td > < td colspan= "1" rowspan= "1" width= "147" > three < / td > < / tr > < tr > < td width= "147" > 1009 < / td > < td width= "147" > Wumao < / td > < td width= "147" > thirteen < / td > < td width= "147" > fourteen < / td > < td colspan= "1" rowspan= "1" width= "147" > three < / td > < / tr > < tr > < td colspan= "1" rowspan= "1" > 1013 < / td > < td colspan= "1" rowspan= "1" > Zhangsan < / td > < td colspan= "1" rowspan= "1" > ten < / td > < td colspan= "1" rowspan= "1" > eleven < / td > < td colspan= "1" rowspan= "1" > four < / td > < / tr > < / tbody > < / table >

Organization chart:

how can it be implemented with php? if you give a UserID value arbitrarily, it can be organized into a multi-dimensional array structure.
for example, if I enter a function aaa (userid=1003), I can return a multi-dimensional array of all descendant nodes under 1003. Use only one query, can not cycle to the database query, the server can not stand. No more than 2 queries, and the results will appear. Return up to 4 layers. For example, enter 1003. Query to 1003, at layer 2, then query to layer 6 at most.

query all descendant nodes of 1003 nodes is like this, left and right value tree reference

SELECT * FROM user_tree WHERE Lft >= 8 AND Lft <= 15 AND Level<=6 ORDER BY Level ASC
array(
    "userid" => "1003",
    "name" => "wlm",
    "children" => array(
        0 => array(
            "userid" => "1007",
            "name" => "buliao",
            "children" => array(
                0 => array(
                    "userid" => "1013",
                    "name" => "zhangsan"
                )
            ) ,
        1 => array(
            "userid" => "1009",
            "name" => "wumao"
            ) ,
        ) ,
    ) ,
);

I"ve been thinking about it for 3 days, and I can"t figure it out, so ask for help from the seniors here.

Apr.26,2021

the result found in the database cannot be more than multidimensional. After finding out, you must calculate the tree according to the left and right weights. It is easy to add a field pid

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb748d-48407.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb748d-48407.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?