3v4l.org

run code in 300+ PHP versions simultaneously
<?php $pages = array(); $pages[1] = array('id' => 1, 'parent' => 0, 'name' => 'Hello World'); $pages[2] = array('id' => 1, 'parent' => 1, 'name' => 'Child of Hello World'); $pages[3] = array('id' => 1, 'parent' => 0, 'name' => 'Brother of Hello World'); $pages[4] = array('id' => 4, 'parent' => 2, 'name' => 'Grand-child of Hello World'); $pages[6] = array('id' => 6, 'parent' => 4, 'name' => 'Great-grand-child of Hello World'); $children = array(); foreach($pages as $key => $page){ $parent = (int)$page['parent']; if(!isset($children[$parent])){ $children[$parent] = array(); $children[$parent][$key] = array('id' => $page['id'], 'name' => $page['name']); } } $new_pages = recursive_append_children($children[0], $children); function recursive_append_children($arr, $children){ foreach($arr as $key => $page) if(isset($children[$key])) $arr[$key]['children'] = recursive_append_children($children[$key], $children); return $arr; } print_r($new_pages);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 31
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 31
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 30
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 30
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/K2BBh
function name:  (null)
number of ops:  43
compiled vars:  !0 = $pages, !1 = $children, !2 = $page, !3 = $key, !4 = $parent, !5 = $new_pages
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    4     1        ASSIGN_DIM                                               !0, 1
          2        OP_DATA                                                  <array>
    5     3        ASSIGN_DIM                                               !0, 2
          4        OP_DATA                                                  <array>
    6     5        ASSIGN_DIM                                               !0, 3
          6        OP_DATA                                                  <array>
    7     7        ASSIGN_DIM                                               !0, 4
          8        OP_DATA                                                  <array>
    8     9        ASSIGN_DIM                                               !0, 6
         10        OP_DATA                                                  <array>
   10    11        ASSIGN                                                   !1, <array>
   11    12      > FE_RESET_R                                       $13     !0, ->31
         13    > > FE_FETCH_R                                       ~14     $13, !2, ->31
         14    >   ASSIGN                                                   !3, ~14
   12    15        FETCH_DIM_R                                      ~16     !2, 'parent'
         16        CAST                                          4  ~17     ~16
         17        ASSIGN                                                   !4, ~17
   13    18        ISSET_ISEMPTY_DIM_OBJ                         0  ~19     !1, !4
         19        BOOL_NOT                                         ~20     ~19
         20      > JMPZ                                                     ~20, ->30
   15    21    >   ASSIGN_DIM                                               !1, !4
         22        OP_DATA                                                  <array>
   16    23        FETCH_DIM_R                                      ~24     !2, 'id'
         24        INIT_ARRAY                                       ~25     ~24, 'id'
         25        FETCH_DIM_R                                      ~26     !2, 'name'
         26        ADD_ARRAY_ELEMENT                                ~25     ~26, 'name'
         27        FETCH_DIM_W                                      $22     !1, !4
         28        ASSIGN_DIM                                               $22, !3
         29        OP_DATA                                                  ~25
   11    30    > > JMP                                                      ->13
         31    >   FE_FREE                                                  $13
   20    32        INIT_FCALL_BY_NAME                                       'recursive_append_children'
         33        CHECK_FUNC_ARG                                           
         34        FETCH_DIM_FUNC_ARG                               $27     !1, 0
         35        SEND_FUNC_ARG                                            $27
         36        SEND_VAR_EX                                              !1
         37        DO_FCALL                                      0  $28     
         38        ASSIGN                                                   !5, $28
   29    39        INIT_FCALL                                               'print_r'
         40        SEND_VAR                                                 !5
         41        DO_ICALL                                                 
         42      > RETURN                                                   1

Function recursive_append_children:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 16
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 16
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/K2BBh
function name:  recursive_append_children
number of ops:  20
compiled vars:  !0 = $arr, !1 = $children, !2 = $page, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   23     2      > FE_RESET_R                                       $4      !0, ->17
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->17
          4    >   ASSIGN                                                   !3, ~5
   24     5        ISSET_ISEMPTY_DIM_OBJ                         0          !1, !3
          6      > JMPZ                                                     ~7, ->16
   25     7    >   INIT_FCALL_BY_NAME                                       'recursive_append_children'
          8        CHECK_FUNC_ARG                                           
          9        FETCH_DIM_FUNC_ARG                               $10     !1, !3
         10        SEND_FUNC_ARG                                            $10
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0  $11     
         13        FETCH_DIM_W                                      $8      !0, !3
         14        ASSIGN_DIM                                               $8, 'children'
         15        OP_DATA                                                  $11
   23    16    > > JMP                                                      ->3
         17    >   FE_FREE                                                  $4
   26    18      > RETURN                                                   !0
   27    19*     > RETURN                                                   null

End of function recursive_append_children

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.44 ms | 1400 KiB | 15 Q