3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [ 1 => [ 11 => [ 111 => ['res_id' => 111, 'city_id' => 11, 'country_id' => 1], 112 => ['res_id' => 112, 'city_id' => 11, 'country_id' => 1], 113 => ['res_id' => 113, 'city_id' => 11, 'country_id' => 1], ], 12 => [ 121 => ['res_id' => 121, 'city_id' => 12, 'country_id' => 1], ], ], 2 => [ 21 => [ 212 => ['res_id' => 212, 'city_id' => 21, 'country_id' => 2], 214 => ['res_id' => 214, 'city_id' => 21, 'country_id' => 2], ], 22 => [ 221 => ['res_id' => 221, 'city_id' => 22, 'country_id' => 2], 222 => ['res_id' => 222, 'city_id' => 22, 'country_id' => 2], 223 => ['res_id' => 223, 'city_id' => 22, 'country_id' => 2], ], ], 3 => [ 31 => [ 312 => ['res_id' => 312, 'city_id' => 21, 'country_id' => 2], 314 => ['res_id' => 314, 'city_id' => 21, 'country_id' => 2], ], ] ]; function array_filter_clean(array $array, array $callbacks, $currentDepth = 0, $currentKey = '') { if (array_key_exists($currentDepth, $callbacks)) { // identify node to apply callback to $callback = $callbacks[$currentDepth]; if (!$callback($currentKey, $array)) { // empty node when callback returns false (or falsy) return []; } } foreach ($array as $key => &$value) { // &value to modify $array if (is_array($value)) { $value = array_filter_clean($value, $callbacks, $currentDepth+1, $key); // recurse if array } } return array_filter($array); // remove empty nodes (you may want to add "afterCallbacks" here) } $callbacksByDepth = [ /* 2 => function ($key, $value) { return $key > 20; }, */ 3 => function ($key, $value) { return $value['res_id']%2; }, ]; $output = array_filter_clean($input, $callbacksByDepth); print_r($output);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ulN1W
function name:  (null)
number of ops:  13
compiled vars:  !0 = $input, !1 = $callbacksByDepth, !2 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   54     1        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
   56     2        INIT_ARRAY                                       ~5      ~4, 3
   50     3        ASSIGN                                                   !1, ~5
   59     4        INIT_FCALL                                               'array_filter_clean'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $7      
          8        ASSIGN                                                   !2, $7
   61     9        INIT_FCALL                                               'print_r'
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                                 
         12      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ulN1W
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $key, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   55     2        FETCH_DIM_R                                      ~2      !1, 'res_id'
          3        MOD                                              ~3      ~2, 2
          4      > RETURN                                                   ~3
   56     5*     > RETURN                                                   null

End of Dynamic Function 0

Function array_filter_clean:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 125) Position 1 = 16, Position 2 = 29
Branch analysis from position: 16
2 jumps found. (Code = 126) Position 1 = 17, Position 2 = 29
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 28
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 28
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
Branch analysis from position: 15
filename:       /in/ulN1W
function name:  array_filter_clean
number of ops:  35
compiled vars:  !0 = $array, !1 = $callbacks, !2 = $currentDepth, !3 = $currentKey, !4 = $callback, !5 = $value, !6 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      0
          3        RECV_INIT                                        !3      ''
   34     4        ARRAY_KEY_EXISTS                                         !2, !1
          5      > JMPZ                                                     ~7, ->15
   35     6    >   FETCH_DIM_R                                      ~8      !1, !2
          7        ASSIGN                                                   !4, ~8
   36     8        INIT_DYNAMIC_CALL                                        !4
          9        SEND_VAR_EX                                              !3
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0  $10     
         12        BOOL_NOT                                         ~11     $10
         13      > JMPZ                                                     ~11, ->15
   37    14    > > RETURN                                                   <array>
   41    15    > > FE_RESET_RW                                      $12     !0, ->29
         16    > > FE_FETCH_RW                                      ~13     $12, !5, ->29
         17    >   ASSIGN                                                   !6, ~13
   42    18        TYPE_CHECK                                  128          !5
         19      > JMPZ                                                     ~15, ->28
   43    20    >   INIT_FCALL_BY_NAME                                       'array_filter_clean'
         21        SEND_VAR_EX                                              !5
         22        SEND_VAR_EX                                              !1
         23        ADD                                              ~16     !2, 1
         24        SEND_VAL_EX                                              ~16
         25        SEND_VAR_EX                                              !6
         26        DO_FCALL                                      0  $17     
         27        ASSIGN                                                   !5, $17
   41    28    > > JMP                                                      ->16
         29    >   FE_FREE                                                  $12
   47    30        INIT_FCALL                                               'array_filter'
         31        SEND_VAR                                                 !0
         32        DO_ICALL                                         $19     
         33      > RETURN                                                   $19
   48    34*     > RETURN                                                   null

End of function array_filter_clean

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
135.15 ms | 1010 KiB | 16 Q