3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ [ 'table' => 'test', 'parent_table' => NULL, ], [ 'table' => 'test', 'parent_table' => NULL, ], [ 'table' => 'test2', 'parent_table' => 'test', ], [ 'table' => 'test4', 'parent_table' => NULL, ], [ 'table' => 'test5', 'parent_table' => 'test3', ], [ 'table' => 'test6', 'parent_table' => 'test5', ], [ 'table' => 'test3', 'parent_table' => 'test', ], ]; function reorderHierarchy($data){ $hierarchy = []; $top_level_parents = []; foreach($data as $each_data){ $hierarchy[$each_data['table']] = array(); if(is_null($each_data['parent_table'])){ if(!isset($top_level_parents[$each_data['table']])){ $top_level_parents[$each_data['table']] = 0; } $top_level_parents[$each_data['table']]++; } } foreach($data as $each_data){ if(!is_null($each_data['parent_table'])){ $hierarchy[$each_data['parent_table']][] = $each_data['table']; } } $result = []; traverseHierarchy($hierarchy,$top_level_parents,$result); return $result; } function traverseHierarchy($hierarchy,$top_level_parents,&$result){ foreach($top_level_parents as $each_parent => $occurrences){ while($occurrences-- > 0){ $result[] = [ 'table' => $each_parent, 'parent_table' => NULL ]; } traverseChildren($hierarchy,$each_parent,$result); } } function traverseChildren($hierarchy,$parent,&$result){ foreach($hierarchy[$parent] as $each_child){ $result[] = [ 'table' => $each_child, 'parent_table' => $parent ]; traverseChildren($hierarchy,$each_child,$result); } } foreach(reorderHierarchy($data) as $each_data){ echo $each_data['table']," , ",(is_null($each_data['parent_table']) ? "NULL" : $each_data['parent_table']),PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 19
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 19
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/AmJpY
function name:  (null)
number of ops:  21
compiled vars:  !0 = $data, !1 = $each_data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   86     1        INIT_FCALL                                               'reorderhierarchy'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4      > FE_RESET_R                                       $4      $3, ->19
          5    > > FE_FETCH_R                                               $4, !1, ->19
   87     6    >   FETCH_DIM_R                                      ~5      !1, 'table'
          7        ECHO                                                     ~5
          8        ECHO                                                     '+%2C+'
          9        FETCH_DIM_R                                      ~6      !1, 'parent_table'
         10        TYPE_CHECK                                    2          ~6
         11      > JMPZ                                                     ~7, ->14
         12    >   QM_ASSIGN                                        ~8      'NULL'
         13      > JMP                                                      ->16
         14    >   FETCH_DIM_R                                      ~9      !1, 'parent_table'
         15        QM_ASSIGN                                        ~8      ~9
         16    >   ECHO                                                     ~8
         17        ECHO                                                     '%0A'
   86    18      > JMP                                                      ->5
         19    >   FE_FREE                                                  $4
   88    20      > RETURN                                                   1

Function reorderhierarchy:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 22
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 22
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 21
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 18
Branch analysis from position: 21
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 35
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 35
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 34
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 34
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
Branch analysis from position: 22
filename:       /in/AmJpY
function name:  reorderHierarchy
number of ops:  44
compiled vars:  !0 = $data, !1 = $hierarchy, !2 = $top_level_parents, !3 = $each_data, !4 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   37     1        ASSIGN                                                   !1, <array>
   38     2        ASSIGN                                                   !2, <array>
   40     3      > FE_RESET_R                                       $7      !0, ->22
          4    > > FE_FETCH_R                                               $7, !3, ->22
   41     5    >   FETCH_DIM_R                                      ~8      !3, 'table'
          6        ASSIGN_DIM                                               !1, ~8
          7        OP_DATA                                                  <array>
   42     8        FETCH_DIM_R                                      ~10     !3, 'parent_table'
          9        TYPE_CHECK                                    2          ~10
         10      > JMPZ                                                     ~11, ->21
   43    11    >   FETCH_DIM_R                                      ~12     !3, 'table'
         12        ISSET_ISEMPTY_DIM_OBJ                         0  ~13     !2, ~12
         13        BOOL_NOT                                         ~14     ~13
         14      > JMPZ                                                     ~14, ->18
   44    15    >   FETCH_DIM_R                                      ~15     !3, 'table'
         16        ASSIGN_DIM                                               !2, ~15
         17        OP_DATA                                                  0
   46    18    >   FETCH_DIM_R                                      ~17     !3, 'table'
         19        FETCH_DIM_RW                                     $18     !2, ~17
         20        PRE_INC                                                  $18
   40    21    > > JMP                                                      ->4
         22    >   FE_FREE                                                  $7
   50    23      > FE_RESET_R                                       $20     !0, ->35
         24    > > FE_FETCH_R                                               $20, !3, ->35
   51    25    >   FETCH_DIM_R                                      ~21     !3, 'parent_table'
         26        TYPE_CHECK                                    2  ~22     ~21
         27        BOOL_NOT                                         ~23     ~22
         28      > JMPZ                                                     ~23, ->34
   52    29    >   FETCH_DIM_R                                      ~24     !3, 'parent_table'
         30        FETCH_DIM_R                                      ~27     !3, 'table'
         31        FETCH_DIM_W                                      $25     !1, ~24
         32        ASSIGN_DIM                                               $25
         33        OP_DATA                                                  ~27
   50    34    > > JMP                                                      ->24
         35    >   FE_FREE                                                  $20
   56    36        ASSIGN                                                   !4, <array>
   57    37        INIT_FCALL_BY_NAME                                       'traverseHierarchy'
         38        SEND_VAR_EX                                              !1
         39        SEND_VAR_EX                                              !2
         40        SEND_VAR_EX                                              !4
         41        DO_FCALL                                      0          
   58    42      > RETURN                                                   !4
   59    43*     > RETURN                                                   null

End of function reorderhierarchy

Function traversehierarchy:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 20
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 7
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 7
Branch analysis from position: 14
Branch analysis from position: 7
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/AmJpY
function name:  traverseHierarchy
number of ops:  22
compiled vars:  !0 = $hierarchy, !1 = $top_level_parents, !2 = $result, !3 = $occurrences, !4 = $each_parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   62     3      > FE_RESET_R                                       $5      !1, ->20
          4    > > FE_FETCH_R                                       ~6      $5, !3, ->20
          5    >   ASSIGN                                                   !4, ~6
   63     6      > JMP                                                      ->11
   65     7    >   INIT_ARRAY                                       ~9      !4, 'table'
          8        ADD_ARRAY_ELEMENT                                ~9      null, 'parent_table'
   64     9        ASSIGN_DIM                                               !2
   65    10        OP_DATA                                                  ~9
   63    11    >   POST_DEC                                         ~10     !3
         12        IS_SMALLER                                               0, ~10
         13      > JMPNZ                                                    ~11, ->7
   70    14    >   INIT_FCALL_BY_NAME                                       'traverseChildren'
         15        SEND_VAR_EX                                              !0
         16        SEND_VAR_EX                                              !4
         17        SEND_VAR_EX                                              !2
         18        DO_FCALL                                      0          
   62    19      > JMP                                                      ->4
         20    >   FE_FREE                                                  $5
   72    21      > RETURN                                                   null

End of function traversehierarchy

Function traversechildren:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 16
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 16
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/AmJpY
function name:  traverseChildren
number of ops:  18
compiled vars:  !0 = $hierarchy, !1 = $parent, !2 = $result, !3 = $each_child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   75     3        FETCH_DIM_R                                      ~4      !0, !1
          4      > FE_RESET_R                                       $5      ~4, ->16
          5    > > FE_FETCH_R                                               $5, !3, ->16
   77     6    >   INIT_ARRAY                                       ~7      !3, 'table'
   78     7        ADD_ARRAY_ELEMENT                                ~7      !1, 'parent_table'
   76     8        ASSIGN_DIM                                               !2
   78     9        OP_DATA                                                  ~7
   81    10        INIT_FCALL_BY_NAME                                       'traverseChildren'
         11        SEND_VAR_EX                                              !0
         12        SEND_VAR_EX                                              !3
         13        SEND_VAR_EX                                              !2
         14        DO_FCALL                                      0          
   75    15      > JMP                                                      ->5
         16    >   FE_FREE                                                  $5
   83    17      > RETURN                                                   null

End of function traversechildren

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.85 ms | 1024 KiB | 14 Q