3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 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 $sub) { if(isset($sub['children']) && FALSE !== ($found = push_at_key($what, $what_key, $key, $sub['children']))) { return $found; } } 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; } //var_dump(v1($array)); var_dump(v2($array));
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/Flp5Q
function name:  (null)
number of ops:  11
compiled vars:  !0 = $array
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   7     0  E >   ASSIGN                                                   !0, <array>
  29     1        NOP                                                      
  46     2        NOP                                                      
  63     3        NOP                                                      
  78     4        INIT_FCALL                                               'var_dump'
         5        INIT_FCALL                                               'v2'
         6        SEND_VAR                                                 !0
         7        DO_FCALL                                      0  $2      
         8        SEND_VAR                                                 $2
         9        DO_ICALL                                                 
        10      > RETURN                                                   1

Function v1:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = 2, Position 2 = 12
Branch analysis from position: 2
Jump found. Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
Jump found. Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
Jump found. Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
Branch analysis from position: 12
Jump found. Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
Jump found. Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
Jump found. Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
Jump found. Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 20
Branch analysis from position: 21
Jump found. Position 1 = -2
Branch analysis from position: 21
Branch analysis from position: 12
filename:       /in/Flp5Q
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                       33554432  ~6      !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
        11    > > JMP                                                      ->2
        12    >   FE_FREE                                                  $3
  35    13        UNSET_VAR                                                !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                       33554432  ~15     !1, 'parent_id'
        18      > JMPZ                                                     ~15, ->20
  40    19    >   UNSET_DIM                                                !0, !2
        20    > > JMP                                                      ->15
        21    >   FE_FREE                                                  $12
  43    22      > RETURN                                                   !0
  44    23*     > RETURN                                                   null

End of function v1

Function push_at_key:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = 5, Position 2 = 16
Branch analysis from position: 5
Jump found. Position 1 = 6, Position 2 = 16
Branch analysis from position: 6
Jump found. Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
Jump found. Position 1 = -2
Branch analysis from position: 15
Jump found. Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 16
Jump found. Position 1 = 18, Position 2 = 35
Branch analysis from position: 18
Jump found. Position 1 = 19, Position 2 = 35
Branch analysis from position: 19
Jump found. Position 1 = 21, Position 2 = 31
Branch analysis from position: 21
Jump found. Position 1 = 32, Position 2 = 34
Branch analysis from position: 32
Jump found. Position 1 = -2
Branch analysis from position: 34
Jump found. Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 31
Branch analysis from position: 35
Jump found. Position 1 = -2
Branch analysis from position: 35
Branch analysis from position: 16
filename:       /in/Flp5Q
function name:  push_at_key
number of ops:  38
compiled vars:  !0 = $what, !1 = $what_key, !2 = $key, !3 = $array, !4 = $v, !5 = $k, !6 = $sub, !7 = $found
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  46     0  E >   RECV                                             !0      
         1        RECV                                             !1      
         2        RECV                                             !2      
         3        RECV                                             !3      
  47     4      > FE_RESET_R                                       $8      !3, ->16
         5    > > FE_FETCH_R                                               $8, !4, ->16
         6    >   ASSIGN                                                   !5, ~9
  48     7        IS_EQUAL                                         ~11     !2, !5
         8      > JMPZ                                                     ~11, ->15
  49     9    >   FETCH_DIM_W                                      $12     !3, !5
        10        FETCH_DIM_W                                      $13     $12, 'children'
        11        ASSIGN_DIM                                               $13, !1
        12        OP_DATA                                                  !0
  50    13        FE_FREE                                                  $8
        14      > RETURN                                                   !3
        15    > > JMP                                                      ->5
        16    >   FE_FREE                                                  $8
  54    17      > FE_RESET_R                                       $15     !3, ->35
        18    > > FE_FETCH_R                                               $15, !6, ->35
  55    19    >   ISSET_ISEMPTY_DIM_OBJ                       33554432  ~16     !6, 'children'
        20      > JMPZ_EX                                          ~16     ~16, ->31
        21    >   INIT_FCALL_BY_NAME                                       'push_at_key'
        22        SEND_VAR_EX                                              !0
        23        SEND_VAR_EX                                              !1
        24        SEND_VAR_EX                                              !2
        25        FETCH_DIM_FUNC_ARG                               $17     !6, 'children'
        26        SEND_VAR_EX                                              $17
        27        DO_FCALL                                      0  $18     
        28        ASSIGN                                           $19     !7, $18
        29        IS_NOT_IDENTICAL                                 ~20     <false>, $19
        30        BOOL                                             ~16     ~20
        31    > > JMPZ                                                     ~16, ->34
  56    32    >   FE_FREE                                                  $15
        33      > RETURN                                                   !7
        34    > > JMP                                                      ->18
        35    >   FE_FREE                                                  $15
  60    36      > RETURN                                                   !3
  61    37*     > RETURN                                                   null

End of function push_at_key

Function v2:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = 3, Position 2 = 20
Branch analysis from position: 3
Jump found. Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
Jump found. Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
Jump found. Position 1 = 19
Branch analysis from position: 19
Jump found. Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
Jump found. Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 20
Jump found. Position 1 = -2
Branch analysis from position: 20
filename:       /in/Flp5Q
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
-------------------------------------------------------------------------------------
  63     0  E >   RECV                                             !0      
  64     1        ASSIGN                                                   !1, <array>
  65     2      > FE_RESET_R                                       $5      !0, ->20
         3    > > FE_FETCH_R                                               $5, !2, ->20
         4    >   ASSIGN                                                   !3, ~6
  66     5        ISSET_ISEMPTY_DIM_OBJ                       33554432  ~8      !2, 'parent_id'
         6        BOOL_NOT                                         ~9      ~8
         7      > JMPZ                                                     ~9, ->11
  67     8    >   ASSIGN_DIM                                               !1, !3
         9        OP_DATA                                                  !2
        10      > JMP                                                      ->19
  70    11    >   INIT_FCALL                                               'push_at_key'
        12        SEND_VAR                                                 !2
        13        SEND_VAR                                                 !3
        14        FETCH_DIM_R                                      $11     !2, 'parent_id'
        15        SEND_VAR                                                 $11
        16        SEND_VAR                                                 !1
        17        DO_FCALL                                      0  $12     
        18        ASSIGN                                                   !1, $12
        19    > > JMP                                                      ->3
        20    >   FE_FREE                                                  $5
  74    21      > RETURN                                                   !1
  75    22*     > RETURN                                                   null

End of function v2

Generated using Vulcan Logic Dumper, using php 7.3.0