3v4l.org

run code in 300+ PHP versions simultaneously
<?php global $array; $array = [ '0' => [ 'id' => 1, 'parent' => 0, 'name' => 'root 0' ], '1' => [ 'id' => 2, 'parent' => 1, 'name' => 'root 1' ], '2' => [ 'id' => 3, 'parent' => 2, // this should have after parse parent 1 'name' => 'root 2' ], '3' => [ 'id' => 4, 'parent' => 3, // this should have after parse parent 1 'name' => 'root 3' ], '4' => [ 'id' => 5, 'parent' => 3, // this should have after parse parent 1 'name' => 'root 4' ], '5' => [ 'id' => 6, 'parent' => 2, // this should have after parse parent 1 'name' => 'root 2' ] ]; global $new_array; $new_array = []; foreach( $array as $item ) { if( $item['parent'] == 0 ) { $new_array[] = $item; // if parent 0 - clone into new array continue; } //echo $item['name'] . PHP_EOL; $temp = check_parent( $item['parent'] ); // get child $item['parent'] = $temp['id']; $new_array[] = $item; } echo '<pre>'; print_r($new_array); function check_parent( $parent ) { //echo '- check for parent of ' . $parent . PHP_EOL; global $array; foreach( $array as $item ) { if( $item['id'] == $parent && $item['parent'] == 0 ) { //echo '[OK] found root parent id: ' . $item['id'] . PHP_EOL; $item['parent'] = $item['id']; return $item; } else { return check_parent( $item['id'] ); } } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 24
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 24
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/82iHY
function name:  (null)
number of ops:  30
compiled vars:  !0 = $array, !1 = $new_array, !2 = $item, !3 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   BIND_GLOBAL                                              !0, 'array'
    4     1        ASSIGN                                                   !0, <array>
   37     2        BIND_GLOBAL                                              !1, 'new_array'
   38     3        ASSIGN                                                   !1, <array>
   40     4      > FE_RESET_R                                       $6      !0, ->24
          5    > > FE_FETCH_R                                               $6, !2, ->24
   42     6    >   FETCH_DIM_R                                      ~7      !2, 'parent'
          7        IS_EQUAL                                                 ~7, 0
          8      > JMPZ                                                     ~8, ->12
   43     9    >   ASSIGN_DIM                                               !1
         10        OP_DATA                                                  !2
   44    11      > JMP                                                      ->5
   48    12    >   INIT_FCALL_BY_NAME                                       'check_parent'
         13        CHECK_FUNC_ARG                                           
         14        FETCH_DIM_FUNC_ARG                               $10     !2, 'parent'
         15        SEND_FUNC_ARG                                            $10
         16        DO_FCALL                                      0  $11     
         17        ASSIGN                                                   !3, $11
   49    18        FETCH_DIM_R                                      ~14     !3, 'id'
         19        ASSIGN_DIM                                               !2, 'parent'
         20        OP_DATA                                                  ~14
   50    21        ASSIGN_DIM                                               !1
         22        OP_DATA                                                  !2
   40    23      > JMP                                                      ->5
         24    >   FE_FREE                                                  $6
   54    25        ECHO                                                     '%3Cpre%3E'
   55    26        INIT_FCALL                                               'print_r'
         27        SEND_VAR                                                 !1
         28        DO_ICALL                                                 
   70    29      > RETURN                                                   1

Function check_parent:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 25
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 25
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 17
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/82iHY
function name:  check_parent
number of ops:  27
compiled vars:  !0 = $parent, !1 = $array, !2 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
   60     1        BIND_GLOBAL                                              !1, 'array'
   61     2      > FE_RESET_R                                       $3      !1, ->25
          3    > > FE_FETCH_R                                               $3, !2, ->25
   62     4    >   FETCH_DIM_R                                      ~4      !2, 'id'
          5        IS_EQUAL                                         ~5      !0, ~4
          6      > JMPZ_EX                                          ~5      ~5, ->10
          7    >   FETCH_DIM_R                                      ~6      !2, 'parent'
          8        IS_EQUAL                                         ~7      ~6, 0
          9        BOOL                                             ~5      ~7
         10    > > JMPZ                                                     ~5, ->17
   64    11    >   FETCH_DIM_R                                      ~9      !2, 'id'
         12        ASSIGN_DIM                                               !2, 'parent'
         13        OP_DATA                                                  ~9
   65    14        FE_FREE                                                  $3
         15      > RETURN                                                   !2
   62    16*       JMP                                                      ->24
   67    17    >   INIT_FCALL_BY_NAME                                       'check_parent'
         18        CHECK_FUNC_ARG                                           
         19        FETCH_DIM_FUNC_ARG                               $10     !2, 'id'
         20        SEND_FUNC_ARG                                            $10
         21        DO_FCALL                                      0  $11     
         22        FE_FREE                                                  $3
         23      > RETURN                                                   $11
   61    24*       JMP                                                      ->3
         25    >   FE_FREE                                                  $3
   70    26      > RETURN                                                   null

End of function check_parent

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.39 ms | 1003 KiB | 14 Q