3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 'children' => [ [ 'id' => 123, 'title' => 'test', 'children' => [ [ 'id' => 345, 'title' => 'test 1', 'children' => [], ], [ 'id' => 567, 'title' => 'test 2', 'children' => [ [ 'id' => 789, 'title' => 'test 3', 'children' => [], ], [ 'id' => 333, 'title' => 'tset 4', 'children' => [ [ 'id' => 222, 'title' => 'test 5', 'children' => [], ], [ 'id' => 111, 'title' => 'test 55', 'children' => [], ], [ 'id' => 444, 'title' => 'test 556', 'children' => [], ], [ 'id' => 666, 'title' => 'test44', 'children' => [], ], ], ], [ 'id' => '5556', 'identifier' => 'test 2', 'children' => [ [ 'id' => 888, 'title' => 'test 2', 'children' => [], ], [ 'id' => 255, 'title' => 'tset', 'children' => [], ], [ 'id' => 454, 'title' => 'warm-up-5837', 'children' => [], ], ], ], ], ], ], ], ], ]; function flattenTree(array $tree): array { $flat = []; foreach ($tree['children'] ?? [] as $child) { $flat[] = ['id' => $child['id'], 'title' => $child['title'] ?? $child['identifier']]; if (!empty($child['children'])) { array_push($flat, ...flattenTree($child, $flat)); } } return $flat; } var_export(flattenTree($array));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/trQdI
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>
   88     1        INIT_FCALL                                               'var_export'
          2        INIT_FCALL                                               'flattentree'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function flattentree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 29
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 29
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 28
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 28
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/trQdI
function name:  flattenTree
number of ops:  34
compiled vars:  !0 = $tree, !1 = $flat, !2 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   RECV                                             !0      
   79     1        ASSIGN                                                   !1, <array>
   80     2        FETCH_DIM_IS                                     ~4      !0, 'children'
          3        COALESCE                                         ~5      ~4
          4        QM_ASSIGN                                        ~5      <array>
          5      > FE_RESET_R                                       $6      ~5, ->29
          6    > > FE_FETCH_R                                               $6, !2, ->29
   81     7    >   FETCH_DIM_R                                      ~8      !2, 'id'
          8        INIT_ARRAY                                       ~9      ~8, 'id'
          9        FETCH_DIM_IS                                     ~10     !2, 'title'
         10        COALESCE                                         ~11     ~10
         11        FETCH_DIM_R                                      ~12     !2, 'identifier'
         12        QM_ASSIGN                                        ~11     ~12
         13        ADD_ARRAY_ELEMENT                                ~9      ~11, 'title'
         14        ASSIGN_DIM                                               !1
         15        OP_DATA                                                  ~9
   82    16        ISSET_ISEMPTY_DIM_OBJ                         1  ~13     !2, 'children'
         17        BOOL_NOT                                         ~14     ~13
         18      > JMPZ                                                     ~14, ->28
   83    19    >   INIT_FCALL                                               'array_push'
         20        SEND_REF                                                 !1
         21        INIT_FCALL_BY_NAME                                       'flattenTree'
         22        SEND_VAR_EX                                              !2
         23        SEND_VAR_EX                                              !1
         24        DO_FCALL                                      0  $15     
         25        SEND_UNPACK                                              $15
         26        CHECK_UNDEF_ARGS                                         
         27        DO_ICALL                                                 
   80    28    > > JMP                                                      ->6
         29    >   FE_FREE                                                  $6
   86    30        VERIFY_RETURN_TYPE                                       !1
         31      > RETURN                                                   !1
   87    32*       VERIFY_RETURN_TYPE                                       
         33*     > RETURN                                                   null

End of function flattentree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
226.25 ms | 1018 KiB | 16 Q