3v4l.org

run code in 300+ PHP versions simultaneously
<?php function freqCount(array $values, $searchValue): array { $frequencies = computeFrequencies($values, $searchValue, 0); return array_map( static function (int $depth) use ($frequencies) { return [$depth, $frequencies[$depth]]; }, array_keys($frequencies) ); } function computeFrequencies(array $values, $searchValue, int $depth): array { $frequencies = [$depth => 0]; foreach ($values as $value) { if ($value === $searchValue) { $frequencies[$depth]++; } elseif (is_array($value)) { foreach (computeFrequencies($value, $searchValue, $depth + 1) as $deeperDepth => $frequency) { $frequencies[$deeperDepth] = ($frequencies[$deeperDepth] ?? 0) + $frequency; } } } return $frequencies; } print_r(freqCount([1, 4, 4, [1, 1, [1, 2, 1, 1]]], 1)); print_r(freqCount([1, 5, 5, [5, [1, 2, 1, 1], 5, 5], 5, [5]], 5)); print_r(freqCount([1, [2], 1, [[2]], 1, [[[2]]], 1, [[[[2]]]]], 2));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M4Ss0
function name:  (null)
number of ops:  22
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   INIT_FCALL                                               'print_r'
          1        INIT_FCALL                                               'freqcount'
          2        SEND_VAL                                                 <array>
          3        SEND_VAL                                                 1
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   33     7        INIT_FCALL                                               'print_r'
          8        INIT_FCALL                                               'freqcount'
          9        SEND_VAL                                                 <array>
         10        SEND_VAL                                                 5
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
   34    14        INIT_FCALL                                               'print_r'
         15        INIT_FCALL                                               'freqcount'
         16        SEND_VAL                                                 <array>
         17        SEND_VAL                                                 2
         18        DO_FCALL                                      0  $4      
         19        SEND_VAR                                                 $4
         20        DO_ICALL                                                 
         21      > RETURN                                                   1

Function freqcount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M4Ss0
function name:  freqCount
number of ops:  21
compiled vars:  !0 = $values, !1 = $searchValue, !2 = $frequencies
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        INIT_FCALL_BY_NAME                                       'computeFrequencies'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        SEND_VAL_EX                                              0
          6        DO_FCALL                                      0  $3      
          7        ASSIGN                                                   !2, $3
    7     8        INIT_FCALL                                               'array_map'
    8     9        DECLARE_LAMBDA_FUNCTION                          ~5      [0]
         10        BIND_LEXICAL                                             ~5, !2
   10    11        SEND_VAL                                                 ~5
   11    12        INIT_FCALL                                               'array_keys'
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $6      
         15        SEND_VAR                                                 $6
    7    16        DO_ICALL                                         $7      
   11    17        VERIFY_RETURN_TYPE                                       $7
         18      > RETURN                                                   $7
   13    19*       VERIFY_RETURN_TYPE                                       
         20*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/M4Ss0
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $depth, !1 = $frequencies
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
    9     2        INIT_ARRAY                                       ~2      !0
          3        FETCH_DIM_R                                      ~3      !1, !0
          4        ADD_ARRAY_ELEMENT                                ~2      ~3
          5      > RETURN                                                   ~2
   10     6*     > RETURN                                                   null

End of Dynamic Function 0

End of function freqcount

Function computefrequencies:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 32
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 32
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 31
Branch analysis from position: 14
2 jumps found. (Code = 77) Position 1 = 21, Position 2 = 30
Branch analysis from position: 21
2 jumps found. (Code = 78) Position 1 = 22, Position 2 = 30
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 30
Branch analysis from position: 31
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/M4Ss0
function name:  computeFrequencies
number of ops:  37
compiled vars:  !0 = $values, !1 = $searchValue, !2 = $depth, !3 = $frequencies, !4 = $value, !5 = $frequency, !6 = $deeperDepth
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   17     3        INIT_ARRAY                                       ~7      0, !2
          4        ASSIGN                                                   !3, ~7
   19     5      > FE_RESET_R                                       $9      !0, ->32
          6    > > FE_FETCH_R                                               $9, !4, ->32
   20     7    >   IS_IDENTICAL                                             !4, !1
          8      > JMPZ                                                     ~10, ->12
   21     9    >   FETCH_DIM_RW                                     $11     !3, !2
         10        PRE_INC                                                  $11
   20    11      > JMP                                                      ->31
   22    12    >   TYPE_CHECK                                  128          !4
         13      > JMPZ                                                     ~13, ->31
   23    14    >   INIT_FCALL_BY_NAME                                       'computeFrequencies'
         15        SEND_VAR_EX                                              !4
         16        SEND_VAR_EX                                              !1
         17        ADD                                              ~14     !2, 1
         18        SEND_VAL_EX                                              ~14
         19        DO_FCALL                                      0  $15     
         20      > FE_RESET_R                                       $16     $15, ->30
         21    > > FE_FETCH_R                                       ~17     $16, !5, ->30
         22    >   ASSIGN                                                   !6, ~17
   24    23        FETCH_DIM_IS                                     ~20     !3, !6
         24        COALESCE                                         ~21     ~20
         25        QM_ASSIGN                                        ~21     0
         26        ADD                                              ~22     ~21, !5
         27        ASSIGN_DIM                                               !3, !6
         28        OP_DATA                                                  ~22
   23    29      > JMP                                                      ->21
         30    >   FE_FREE                                                  $16
   19    31    > > JMP                                                      ->6
         32    >   FE_FREE                                                  $9
   29    33        VERIFY_RETURN_TYPE                                       !3
         34      > RETURN                                                   !3
   30    35*       VERIFY_RETURN_TYPE                                       
         36*     > RETURN                                                   null

End of function computefrequencies

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.02 ms | 1024 KiB | 19 Q