3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = array( 55 => array( 'ident' => 'test 1', 'depth' => 1, ), 77 => array( 'parent_id' => 55, 'ident' => 'test 2', 'depth' => 2, ), 109 => array( 'parent_id' => 77, 'ident' => 'test 3', 'depth' => 3, ), 78 => array( 'parent_id' => 55, 'ident' => 'test 4', 'depth' => 2, ), 25 => array( 'ident' => 'test 5', 'depth' => 1, ) ); 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 &find_by_key($key, $array) { var_dump($array); foreach($array as $k => &$v) { if($key == $k) { return $v; } } foreach($array as $sub) { if(isset($sub['children']) && FALSE !== ($found = &find_by_key($key, $sub['children']))) { return $found; } } return FALSE; } function v2($array) { $out = array(); foreach ($array as $key => $sub) { if(!isset($sub['parent_id'])) { $out[$key] = $sub; $out[$key]['children'] = array(); } else { $parent = &find_by_key($sub['parent_id'], $out); $parent['children'] = array(); $parent['children'][$key] = $sub; return $out; } } return $out; } //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/7cd5F
function name:  (null)
number of ops:  8
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   82     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'v2'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

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/7cd5F
function name:  v1
number of ops:  24
compiled vars:  !0 = $array, !1 = $sub, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   30     1      > FE_RESET_RW                                      $3      !0, ->12
          2    > > FE_FETCH_RW                                      ~4      $3, !1, ->12
          3    >   ASSIGN                                                   !2, ~4
   31     4        ISSET_ISEMPTY_DIM_OBJ                         0          !1, 'parent_id'
          5      > JMPZ                                                     ~6, ->11
   32     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
   30    11    > > JMP                                                      ->2
         12    >   FE_FREE                                                  $3
   35    13        UNSET_CV                                                 !1
   38    14      > FE_RESET_R                                       $12     !0, ->21
         15    > > FE_FETCH_R                                       ~13     $12, !1, ->21
         16    >   ASSIGN                                                   !2, ~13
   39    17        ISSET_ISEMPTY_DIM_OBJ                         0          !1, 'parent_id'
         18      > JMPZ                                                     ~15, ->20
   40    19    >   UNSET_DIM                                                !0, !2
   38    20    > > JMP                                                      ->15
         21    >   FE_FREE                                                  $12
   43    22      > RETURN                                                   !0
   44    23*     > RETURN                                                   null

End of function v1

Function find_by_key:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
2 jumps found. (Code = 126) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
Return found
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 13
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 31
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 31
Branch analysis from position: 16
2 jumps found. (Code = 46) Position 1 = 18, Position 2 = 27
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
Return found
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 27
Branch analysis from position: 31
Return found
Branch analysis from position: 31
Branch analysis from position: 13
filename:       /in/7cd5F
function name:  find_by_key
number of ops:  34
compiled vars:  !0 = $key, !1 = $array, !2 = $v, !3 = $k, !4 = $sub, !5 = $found
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   47     2        INIT_FCALL                                               'var_dump'
          3        SEND_VAR                                                 !1
          4        DO_ICALL                                                 
   48     5      > FE_RESET_RW                                      $7      !1, ->13
          6    > > FE_FETCH_RW                                      ~8      $7, !2, ->13
          7    >   ASSIGN                                                   !3, ~8
   49     8        IS_EQUAL                                                 !0, !3
          9      > JMPZ                                                     ~10, ->12
   50    10    >   FE_FREE                                                  $7
         11      > RETURN_BY_REF                                            !2
   48    12    > > JMP                                                      ->6
         13    >   FE_FREE                                                  $7
   54    14      > FE_RESET_R                                       $11     !1, ->31
         15    > > FE_FETCH_R                                               $11, !4, ->31
   55    16    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~12     !4, 'children'
         17      > JMPZ_EX                                          ~12     ~12, ->27
         18    >   INIT_FCALL_BY_NAME                                       'find_by_key'
         19        SEND_VAR_EX                                              !0
         20        CHECK_FUNC_ARG                                           
         21        FETCH_DIM_FUNC_ARG                               $13     !4, 'children'
         22        SEND_FUNC_ARG                                            $13
         23        DO_FCALL                                      0  $14     
         24        ASSIGN_REF                                       $15     !5, $14
         25        TYPE_CHECK                                  1018  ~16     $15
         26        BOOL                                             ~12     ~16
         27    > > JMPZ                                                     ~12, ->30
   56    28    >   FE_FREE                                                  $11
         29      > RETURN_BY_REF                                            !5
   54    30    > > JMP                                                      ->15
         31    >   FE_FREE                                                  $11
   60    32      > RETURN_BY_REF                                            <false>
   61    33*     > RETURN_BY_REF                                            null

End of function find_by_key

Function v2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 28
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 28
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/7cd5F
function name:  v2
number of ops:  31
compiled vars:  !0 = $array, !1 = $out, !2 = $sub, !3 = $key, !4 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
   64     1        ASSIGN                                                   !1, <array>
   65     2      > FE_RESET_R                                       $6      !0, ->28
          3    > > FE_FETCH_R                                       ~7      $6, !2, ->28
          4    >   ASSIGN                                                   !3, ~7
   66     5        ISSET_ISEMPTY_DIM_OBJ                         0  ~9      !2, 'parent_id'
          6        BOOL_NOT                                         ~10     ~9
          7      > JMPZ                                                     ~10, ->14
   67     8    >   ASSIGN_DIM                                               !1, !3
          9        OP_DATA                                                  !2
   68    10        FETCH_DIM_W                                      $12     !1, !3
         11        ASSIGN_DIM                                               $12, 'children'
         12        OP_DATA                                                  <array>
         13      > JMP                                                      ->27
   71    14    >   INIT_FCALL                                               'find_by_key'
         15        FETCH_DIM_R                                      ~14     !2, 'parent_id'
         16        SEND_VAL                                                 ~14
         17        SEND_VAR                                                 !1
         18        DO_FCALL                                      0  $15     
         19        ASSIGN_REF                                               !4, $15
   72    20        ASSIGN_DIM                                               !4, 'children'
         21        OP_DATA                                                  <array>
   73    22        FETCH_DIM_W                                      $18     !4, 'children'
         23        ASSIGN_DIM                                               $18, !3
         24        OP_DATA                                                  !2
   74    25        FE_FREE                                                  $6
         26      > RETURN                                                   !1
   65    27    > > JMP                                                      ->3
         28    >   FE_FREE                                                  $6
   78    29      > RETURN                                                   !1
   79    30*     > RETURN                                                   null

End of function v2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.89 ms | 1410 KiB | 17 Q