3v4l.org

run code in 300+ PHP versions simultaneously
<?php function findParent(&$array,$whos_your_daddy=0,$baby=[]){ // make $array modifiable foreach($array as $i=>&$row){ // make $row modifiable if($whos_your_daddy){ if($row['id']==$whos_your_daddy){ // found parent $row['nodes'][]=$baby; }elseif(isset($row['nodes'])){ // go down rabbit hole looking for parent findParent($row['nodes'],$whos_your_daddy,$baby); } }elseif($row['parent_id']){ // child requires adoption unset($array[$i]); // remove child from level because it will be store elsewhere and won't be its own parent (reduce iterations in next loop & avoid infinite recursion) findParent($array,$row['parent_id'],$row); } } return $array; } // $db->query('SELECT id,name_a AS name,parent_id FROM accounts_tree ORDER BY id'); // for($resultset=[]; $row=$res->fetch_assoc(); $resultset[]=$row); // inspired by: http://php.net/manual/en/mysqli-result.fetch-assoc.php#112924 $resultset=[ ['id'=>1,'name'=>'folder 1','parent_id'=>0], ['id'=>2,'name'=>'folder 2','parent_id'=>0], ['id'=>3,'name'=>'sub 1-1','parent_id'=>1], ['id'=>4,'name'=>'sub 2-1','parent_id'=>2], ['id'=>5,'name'=>'Sub 1-1-1','parent_id'=>3], ['id'=>6,'name'=>'folder 3','parent_id'=>0], ['id'=>7,'name'=>'sub 1-1-1-1','parent_id'=>5] ]; print_r(findParent($resultset));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7IB3U
function name:  (null)
number of ops:  8
compiled vars:  !0 = $resultset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   ASSIGN                                                   !0, <array>
   32     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'findparent'
          3        SEND_REF                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function findparent:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 4, Position 2 = 35
Branch analysis from position: 4
2 jumps found. (Code = 126) Position 1 = 5, Position 2 = 35
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 24
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 23
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 23
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 34
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 34
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
filename:       /in/7IB3U
function name:  findParent
number of ops:  38
compiled vars:  !0 = $array, !1 = $whos_your_daddy, !2 = $baby, !3 = $row, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV_INIT                                        !2      <array>
    3     3      > FE_RESET_RW                                      $5      !0, ->35
          4    > > FE_FETCH_RW                                      ~6      $5, !3, ->35
          5    >   ASSIGN                                                   !4, ~6
    4     6      > JMPZ                                                     !1, ->24
    5     7    >   FETCH_DIM_R                                      ~8      !3, 'id'
          8        IS_EQUAL                                                 !1, ~8
          9      > JMPZ                                                     ~9, ->14
    6    10    >   FETCH_DIM_W                                      $10     !3, 'nodes'
         11        ASSIGN_DIM                                               $10
         12        OP_DATA                                                  !2
         13      > JMP                                                      ->23
    7    14    >   ISSET_ISEMPTY_DIM_OBJ                         0          !3, 'nodes'
         15      > JMPZ                                                     ~12, ->23
    8    16    >   INIT_FCALL_BY_NAME                                       'findParent'
         17        CHECK_FUNC_ARG                                           
         18        FETCH_DIM_FUNC_ARG                               $13     !3, 'nodes'
         19        SEND_FUNC_ARG                                            $13
         20        SEND_VAR_EX                                              !1
         21        SEND_VAR_EX                                              !2
         22        DO_FCALL                                      0          
         23    > > JMP                                                      ->34
   10    24    >   FETCH_DIM_R                                      ~15     !3, 'parent_id'
         25      > JMPZ                                                     ~15, ->34
   11    26    >   UNSET_DIM                                                !0, !4
   12    27        INIT_FCALL_BY_NAME                                       'findParent'
         28        SEND_VAR_EX                                              !0
         29        CHECK_FUNC_ARG                                           
         30        FETCH_DIM_FUNC_ARG                               $16     !3, 'parent_id'
         31        SEND_FUNC_ARG                                            $16
         32        SEND_VAR_EX                                              !3
         33        DO_FCALL                                      0          
    3    34    > > JMP                                                      ->4
         35    >   FE_FREE                                                  $5
   15    36      > RETURN                                                   !0
   16    37*     > RETURN                                                   null

End of function findparent

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.62 ms | 1403 KiB | 16 Q