3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = array(array('table' => 'test', 'parent_table' => NULL), array('table' => 'test', 'parent_table' => NULL), array('table' => 'test2', 'parent_table' => 'test'), array('table' => 'test4', 'parent_table' => NULL), array('table' => 'test5', 'parent_table' => 'test3'), array('table' => 'test6', 'parent_table' => 'test5'), array('table' => 'test3', 'parent_table' => 'test')); function hsort($array, $parent) { $output = array(); $children = array_filter($array, function ($v) use ($parent) { return $v['parent_table'] === $parent; }); sort($children); $lastchild = NULL; foreach ($children as $child) { if ($child != $lastchild && !is_null($lastchild)) { $output[] = $lastchild; $output = array_merge($output, hsort($array, $lastchild['table'])); } elseif ($lastchild != NULL) { $output[] = $lastchild; } $lastchild = $child; } if (!is_null($lastchild)) { $output[] = $lastchild; $output = array_merge($output, hsort($array, $lastchild['table'])); } return $output; } echo "table | parent_table\n"; foreach (hsort($array, NULL) as $v) { printf("%-8s| %s\n", $v['table'], $v['parent_table'] ?? 'NULL'); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 18
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/PY6O9
function name:  (null)
number of ops:  20
compiled vars:  !0 = $array, !1 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   32     1        ECHO                                                     'table+++%7C+parent_table%0A'
   33     2        INIT_FCALL                                               'hsort'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 null
          5        DO_FCALL                                      0  $3      
          6      > FE_RESET_R                                       $4      $3, ->18
          7    > > FE_FETCH_R                                               $4, !1, ->18
   34     8    >   INIT_FCALL                                               'printf'
          9        SEND_VAL                                                 '%25-8s%7C+%25s%0A'
         10        FETCH_DIM_R                                      ~5      !1, 'table'
         11        SEND_VAL                                                 ~5
         12        FETCH_DIM_IS                                     ~6      !1, 'parent_table'
         13        COALESCE                                         ~7      ~6
         14        QM_ASSIGN                                        ~7      'NULL'
         15        SEND_VAL                                                 ~7
         16        DO_ICALL                                                 
   33    17      > JMP                                                      ->7
         18    >   FE_FREE                                                  $4
   35    19      > RETURN                                                   1

Function hsort:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 42
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 42
Branch analysis from position: 16
2 jumps found. (Code = 46) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 36
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 40
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 40
Branch analysis from position: 21
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 59
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 59
Branch analysis from position: 42
filename:       /in/PY6O9
function name:  hsort
number of ops:  61
compiled vars:  !0 = $array, !1 = $parent, !2 = $output, !3 = $children, !4 = $lastchild, !5 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        ASSIGN                                                   !2, <array>
   12     3        INIT_FCALL                                               'array_filter'
          4        SEND_VAR                                                 !0
          5        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
          6        BIND_LEXICAL                                             ~7, !1
          7        SEND_VAL                                                 ~7
          8        DO_ICALL                                         $8      
          9        ASSIGN                                                   !3, $8
   13    10        INIT_FCALL                                               'sort'
         11        SEND_REF                                                 !3
         12        DO_ICALL                                                 
   14    13        ASSIGN                                                   !4, null
   15    14      > FE_RESET_R                                       $12     !3, ->42
         15    > > FE_FETCH_R                                               $12, !5, ->42
   16    16    >   IS_NOT_EQUAL                                     ~13     !5, !4
         17      > JMPZ_EX                                          ~13     ~13, ->21
         18    >   TYPE_CHECK                                    2  ~14     !4
         19        BOOL_NOT                                         ~15     ~14
         20        BOOL                                             ~13     ~15
         21    > > JMPZ                                                     ~13, ->36
   17    22    >   ASSIGN_DIM                                               !2
         23        OP_DATA                                                  !4
   18    24        INIT_FCALL                                               'array_merge'
         25        SEND_VAR                                                 !2
         26        INIT_FCALL_BY_NAME                                       'hsort'
         27        SEND_VAR_EX                                              !0
         28        CHECK_FUNC_ARG                                           
         29        FETCH_DIM_FUNC_ARG                               $17     !4, 'table'
         30        SEND_FUNC_ARG                                            $17
         31        DO_FCALL                                      0  $18     
         32        SEND_VAR                                                 $18
         33        DO_ICALL                                         $19     
         34        ASSIGN                                                   !2, $19
   16    35      > JMP                                                      ->40
   20    36    >   IS_NOT_EQUAL                                             !4, null
         37      > JMPZ                                                     ~21, ->40
   21    38    >   ASSIGN_DIM                                               !2
         39        OP_DATA                                                  !4
   23    40    >   ASSIGN                                                   !4, !5
   15    41      > JMP                                                      ->15
         42    >   FE_FREE                                                  $12
   25    43        TYPE_CHECK                                    2  ~24     !4
         44        BOOL_NOT                                         ~25     ~24
         45      > JMPZ                                                     ~25, ->59
   26    46    >   ASSIGN_DIM                                               !2
         47        OP_DATA                                                  !4
   27    48        INIT_FCALL                                               'array_merge'
         49        SEND_VAR                                                 !2
         50        INIT_FCALL_BY_NAME                                       'hsort'
         51        SEND_VAR_EX                                              !0
         52        CHECK_FUNC_ARG                                           
         53        FETCH_DIM_FUNC_ARG                               $27     !4, 'table'
         54        SEND_FUNC_ARG                                            $27
         55        DO_FCALL                                      0  $28     
         56        SEND_VAR                                                 $28
         57        DO_ICALL                                         $29     
         58        ASSIGN                                                   !2, $29
   29    59    > > RETURN                                                   !2
   30    60*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PY6O9
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $v, !1 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_DIM_R                                      ~2      !0, 'parent_table'
          3        IS_IDENTICAL                                     ~3      !1, ~2
          4      > RETURN                                                   ~3
          5*     > RETURN                                                   null

End of Dynamic Function 0

End of function hsort

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.66 ms | 1012 KiB | 18 Q