3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = array ( array( 'id' => '2', 'name' => 'Item2', 'children' => array( array( 'id' => '1', 'name' => 'Item1', 'children' => array( array('id' => '5', 'name' => 'Item5') ) ), array( 'id' => '4', 'name' => 'Item4' ) ) ), array( 'id' => '3', 'name' => 'Item3' ) ); function list_items($array, $parent = 0) { $output = array(); foreach ($array as $arr) { $output[] = array('id' => $arr['id'], 'name' => $arr['name'], 'parent' => $parent); if (is_array($arr['children'] ?? NULL)) { $output = array_merge($output, list_items($arr['children'], $arr['id'])); } } return $output; } $items = list_items($array); print_r($items);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7Pdhu
function name:  (null)
number of ops:  9
compiled vars:  !0 = $array, !1 = $items
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   37     1        INIT_FCALL                                               'list_items'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   38     5        INIT_FCALL                                               'print_r'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                                 
          8      > RETURN                                                   1

Function list_items:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 31
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 31
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 30
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
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/7Pdhu
function name:  list_items
number of ops:  34
compiled vars:  !0 = $array, !1 = $parent, !2 = $output, !3 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
   27     2        ASSIGN                                                   !2, <array>
   28     3      > FE_RESET_R                                       $5      !0, ->31
          4    > > FE_FETCH_R                                               $5, !3, ->31
   29     5    >   FETCH_DIM_R                                      ~7      !3, 'id'
          6        INIT_ARRAY                                       ~8      ~7, 'id'
          7        FETCH_DIM_R                                      ~9      !3, 'name'
          8        ADD_ARRAY_ELEMENT                                ~8      ~9, 'name'
          9        ADD_ARRAY_ELEMENT                                ~8      !1, 'parent'
         10        ASSIGN_DIM                                               !2
         11        OP_DATA                                                  ~8
   30    12        FETCH_DIM_IS                                     ~10     !3, 'children'
         13        COALESCE                                         ~11     ~10
         14        QM_ASSIGN                                        ~11     null
         15        TYPE_CHECK                                  128          ~11
         16      > JMPZ                                                     ~12, ->30
   31    17    >   INIT_FCALL                                               'array_merge'
         18        SEND_VAR                                                 !2
         19        INIT_FCALL_BY_NAME                                       'list_items'
         20        CHECK_FUNC_ARG                                           
         21        FETCH_DIM_FUNC_ARG                               $13     !3, 'children'
         22        SEND_FUNC_ARG                                            $13
         23        CHECK_FUNC_ARG                                           
         24        FETCH_DIM_FUNC_ARG                               $14     !3, 'id'
         25        SEND_FUNC_ARG                                            $14
         26        DO_FCALL                                      0  $15     
         27        SEND_VAR                                                 $15
         28        DO_ICALL                                         $16     
         29        ASSIGN                                                   !2, $16
   28    30    > > JMP                                                      ->4
         31    >   FE_FREE                                                  $5
   34    32      > RETURN                                                   !2
   35    33*     > RETURN                                                   null

End of function list_items

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.85 ms | 1453 KiB | 16 Q