3v4l.org

run code in 300+ PHP versions simultaneously
<?php function replaceParent(&$array, $parent = null) { foreach ($array as &$item) { if ($item['id'] == $parent) { if ($item['parent']) { return replaceParent($array, $item['parent']); } else { return $item['id']; } } elseif ($item['parent']) { $item['parent'] = replaceParent($array, $item['parent']); } } } $array = [ '0' => [ 'id' => 1, 'parent' => 0, 'name' => 'root 0' ], '1' => [ 'id' => 2, 'parent' => 1, 'name' => 'root 1' ], '2' => [ 'id' => 3, 'parent' => 2, 'name' => 'root 2' ], '3' => [ 'id' => 4, 'parent' => 3, 'name' => 'root 3' ], '4' => [ 'id' => 5, 'parent' => 3, 'name' => 'root 4' ], '5' => [ 'id' => 6, 'parent' => 2, 'name' => 'root 2' ] ]; replaceParent($array); var_export($array);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ftCHb
function name:  (null)
number of ops:  8
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   ASSIGN                                                   !0, <array>
   49     1        INIT_FCALL                                               'replaceparent'
          2        SEND_REF                                                 !0
          3        DO_FCALL                                      0          
   50     4        INIT_FCALL                                               'var_export'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

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

End of function replaceparent

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
199.15 ms | 1005 KiB | 15 Q