3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ 'one' => '...', 'two' => [ 'three' => '...', 'four' => '...', 'five' => [ 'six' => '...', 'seven' => '...', ], ], ]; $fields = [ 'one', 'two' => [ 'four', 'five' => [ 'seven', ], ], ]; var_dump(filterRecursive($data, $fields)); function filterRecursive(array $data, array $fields) { foreach ( $data as $key => &$value ) { if ( is_array($fields[$key]) ) { $value = filterRecursive($value, $fields[$key]); } elseif ( !in_array($key, $fields) ) { unset($value); } } return $data; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Cust8
function name:  (null)
number of ops:  10
compiled vars:  !0 = $data, !1 = $fields
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   15     1        ASSIGN                                                   !1, <array>
   25     2        INIT_FCALL                                               'var_dump'
          3        INIT_FCALL_BY_NAME                                       'filterRecursive'
          4        SEND_VAR_EX                                              !0
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
   38     9      > RETURN                                                   1

Function filterrecursive:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 3, Position 2 = 24
Branch analysis from position: 3
2 jumps found. (Code = 126) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 23
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 23
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/Cust8
function name:  filterRecursive
number of ops:  27
compiled vars:  !0 = $data, !1 = $fields, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   29     2      > FE_RESET_RW                                      $4      !0, ->24
          3    > > FE_FETCH_RW                                      ~5      $4, !2, ->24
          4    >   ASSIGN                                                   !3, ~5
   30     5        FETCH_DIM_R                                      ~7      !1, !3
          6        TYPE_CHECK                                  128          ~7
          7      > JMPZ                                                     ~8, ->16
   31     8    >   INIT_FCALL_BY_NAME                                       'filterRecursive'
          9        SEND_VAR_EX                                              !2
         10        CHECK_FUNC_ARG                                           
         11        FETCH_DIM_FUNC_ARG                               $9      !1, !3
         12        SEND_FUNC_ARG                                            $9
         13        DO_FCALL                                      0  $10     
         14        ASSIGN                                                   !2, $10
         15      > JMP                                                      ->23
   32    16    >   INIT_FCALL                                               'in_array'
         17        SEND_VAR                                                 !3
         18        SEND_VAR                                                 !1
         19        DO_ICALL                                         $12     
         20        BOOL_NOT                                         ~13     $12
         21      > JMPZ                                                     ~13, ->23
   33    22    >   UNSET_CV                                                 !2
   29    23    > > JMP                                                      ->3
         24    >   FE_FREE                                                  $4
   37    25      > RETURN                                                   !0
   38    26*     > RETURN                                                   null

End of function filterrecursive

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.88 ms | 1392 KiB | 17 Q