3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gen($parents, $children) { $data = array(); $i = 0; for($i; $i < $parents; $i++) { $data[$i] = array( 'indent' => 'test ' . $i, 'depth' => 1 ); } for($i; $i < $children; $i++) { $data[$i] = array( 'indent' => 'test ' . $i, 'depth' => 'x', 'parent_id' => rand(0, $i) ); } return $data; } function v1($array) { foreach ($array as $key => &$sub) { if (isset($sub['parent_id'])) { $array[$sub['parent_id']]['children'][$key] = &$sub; } } unset($sub); // unset the reference to make sure to not overwrite it later... // now remove the entries with parents foreach ($array as $key => $sub) { if (isset($sub['parent_id'])) unset($array[$key]); } return $array; } function push_at_key($what, $what_key, $key, $array) { foreach($array as $k => $v) { if($key == $k) { $array[$k]['children'][$what_key] = $what; return $array; } } foreach($array as $k => $v) { $array[$k] = push_at_key($what, $what_key, $key, $v['children']); } return $array; } function v2($array) { $out = array(); foreach ($array as $key => $sub) { if(!isset($sub['parent_id'])) { $out[$key] = $sub; } else { $out = push_at_key($sub, $key, $sub['parent_id'], $out); } } return $out; } $array = gen(10, 690); var_dump(v1($array)); //var_dump(v2($array));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LVnCP
function name:  (null)
number of ops:  12
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   INIT_FCALL                                               'gen'
          1        SEND_VAL                                                 10
          2        SEND_VAL                                                 690
          3        DO_FCALL                                      0  $1      
          4        ASSIGN                                                   !0, $1
   75     5        INIT_FCALL                                               'var_dump'
          6        INIT_FCALL                                               'v1'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR                                                 $3
         10        DO_ICALL                                                 
   76    11      > RETURN                                                   1

Function gen:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 5
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 14
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 14
Branch analysis from position: 27
Branch analysis from position: 14
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 5
Branch analysis from position: 13
Branch analysis from position: 5
filename:       /in/LVnCP
function name:  gen
number of ops:  29
compiled vars:  !0 = $parents, !1 = $children, !2 = $data, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        ASSIGN                                                   !2, <array>
    6     3        ASSIGN                                                   !3, 0
    8     4      > JMP                                                      ->11
   10     5    >   CONCAT                                           ~7      'test+', !3
          6        INIT_ARRAY                                       ~8      ~7, 'indent'
   11     7        ADD_ARRAY_ELEMENT                                ~8      1, 'depth'
    9     8        ASSIGN_DIM                                               !2, !3
   11     9        OP_DATA                                                  ~8
    8    10        PRE_INC                                                  !3
         11    >   IS_SMALLER                                               !3, !0
         12      > JMPNZ                                                    ~10, ->5
   15    13    > > JMP                                                      ->25
   17    14    >   CONCAT                                           ~12     'test+', !3
         15        INIT_ARRAY                                       ~13     ~12, 'indent'
   18    16        ADD_ARRAY_ELEMENT                                ~13     'x', 'depth'
   19    17        INIT_FCALL                                               'rand'
         18        SEND_VAL                                                 0
         19        SEND_VAR                                                 !3
         20        DO_ICALL                                         $14     
         21        ADD_ARRAY_ELEMENT                                ~13     $14, 'parent_id'
   16    22        ASSIGN_DIM                                               !2, !3
   19    23        OP_DATA                                                  ~13
   15    24        PRE_INC                                                  !3
         25    >   IS_SMALLER                                               !3, !1
         26      > JMPNZ                                                    ~16, ->14
   23    27    > > RETURN                                                   !2
   24    28*     > RETURN                                                   null

End of function gen

Function v1:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 2, Position 2 = 12
Branch analysis from position: 2
2 jumps found. (Code = 126) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 20
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
Branch analysis from position: 12
filename:       /in/LVnCP
function name:  v1
number of ops:  24
compiled vars:  !0 = $array, !1 = $sub, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   28     1      > FE_RESET_RW                                      $3      !0, ->12
          2    > > FE_FETCH_RW                                      ~4      $3, !1, ->12
          3    >   ASSIGN                                                   !2, ~4
   29     4        ISSET_ISEMPTY_DIM_OBJ                         0          !1, 'parent_id'
          5      > JMPZ                                                     ~6, ->11
   30     6    >   FETCH_DIM_R                                      ~7      !1, 'parent_id'
          7        FETCH_DIM_W                                      $8      !0, ~7
          8        FETCH_DIM_W                                      $9      $8, 'children'
          9        FETCH_DIM_W                                      $10     $9, !2
         10        ASSIGN_REF                                               $10, !1
   28    11    > > JMP                                                      ->2
         12    >   FE_FREE                                                  $3
   33    13        UNSET_CV                                                 !1
   36    14      > FE_RESET_R                                       $12     !0, ->21
         15    > > FE_FETCH_R                                       ~13     $12, !1, ->21
         16    >   ASSIGN                                                   !2, ~13
   37    17        ISSET_ISEMPTY_DIM_OBJ                         0          !1, 'parent_id'
         18      > JMPZ                                                     ~15, ->20
   38    19    >   UNSET_DIM                                                !0, !2
   36    20    > > JMP                                                      ->15
         21    >   FE_FREE                                                  $12
   41    22      > RETURN                                                   !0
   42    23*     > RETURN                                                   null

End of function v1

Function push_at_key:
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
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 16
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 31
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 31
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
Branch analysis from position: 16
filename:       /in/LVnCP
function name:  push_at_key
number of ops:  34
compiled vars:  !0 = $what, !1 = $what_key, !2 = $key, !3 = $array, !4 = $v, !5 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   45     4      > FE_RESET_R                                       $6      !3, ->16
          5    > > FE_FETCH_R                                       ~7      $6, !4, ->16
          6    >   ASSIGN                                                   !5, ~7
   46     7        IS_EQUAL                                                 !2, !5
          8      > JMPZ                                                     ~9, ->15
   47     9    >   FETCH_DIM_W                                      $10     !3, !5
         10        FETCH_DIM_W                                      $11     $10, 'children'
         11        ASSIGN_DIM                                               $11, !1
         12        OP_DATA                                                  !0
   48    13        FE_FREE                                                  $6
         14      > RETURN                                                   !3
   45    15    > > JMP                                                      ->5
         16    >   FE_FREE                                                  $6
   52    17      > FE_RESET_R                                       $13     !3, ->31
         18    > > FE_FETCH_R                                       ~14     $13, !4, ->31
         19    >   ASSIGN                                                   !5, ~14
   53    20        INIT_FCALL_BY_NAME                                       'push_at_key'
         21        SEND_VAR_EX                                              !0
         22        SEND_VAR_EX                                              !1
         23        SEND_VAR_EX                                              !2
         24        CHECK_FUNC_ARG                                           
         25        FETCH_DIM_FUNC_ARG                               $17     !4, 'children'
         26        SEND_FUNC_ARG                                            $17
         27        DO_FCALL                                      0  $18     
         28        ASSIGN_DIM                                               !3, !5
         29        OP_DATA                                                  $18
   52    30      > JMP                                                      ->18
         31    >   FE_FREE                                                  $13
   56    32      > RETURN                                                   !3
   57    33*     > RETURN                                                   null

End of function push_at_key

Function v2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 20
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/LVnCP
function name:  v2
number of ops:  23
compiled vars:  !0 = $array, !1 = $out, !2 = $sub, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   60     1        ASSIGN                                                   !1, <array>
   61     2      > FE_RESET_R                                       $5      !0, ->20
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->20
          4    >   ASSIGN                                                   !3, ~6
   62     5        ISSET_ISEMPTY_DIM_OBJ                         0  ~8      !2, 'parent_id'
          6        BOOL_NOT                                         ~9      ~8
          7      > JMPZ                                                     ~9, ->11
   63     8    >   ASSIGN_DIM                                               !1, !3
          9        OP_DATA                                                  !2
         10      > JMP                                                      ->19
   66    11    >   INIT_FCALL                                               'push_at_key'
         12        SEND_VAR                                                 !2
         13        SEND_VAR                                                 !3
         14        FETCH_DIM_R                                      ~11     !2, 'parent_id'
         15        SEND_VAL                                                 ~11
         16        SEND_VAR                                                 !1
         17        DO_FCALL                                      0  $12     
         18        ASSIGN                                                   !1, $12
   61    19    > > JMP                                                      ->3
         20    >   FE_FREE                                                  $5
   70    21      > RETURN                                                   !1
   71    22*     > RETURN                                                   null

End of function v2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.79 ms | 1411 KiB | 20 Q