3v4l.org

run code in 500+ PHP versions simultaneously
<?php // median code: //http://www.mdj.us/web-development/php-programming/calculating-the-median-average-values-of-an-array-with-php/ function calculate_median($arr) { sort($arr); $count = count($arr); //total numbers in array $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value if($count % 2) { // odd number, middle is the median $median = $arr[$middleval]; } else { // even number, calculate avg of 2 medians $low = $arr[$middleval]; $high = $arr[$middleval+1]; $median = (($low+$high)/2); } return $median; } $a = []; $a["2017-05-01"] = ["DC"]; $a["2017-05-01"]["DC"]["IT"] = [90,0]; $a["2017-05-01"]["DC"]["DE"] = [18,315,40,"",69]; $a["2017-05-01"]["DC"]["Other"] = [107,46,"","",27,22]; function sort_by_order ($a, $b) { if ($a == "") $a = 0; if ($b == "") $b = 0; return $a - $b; } function test($item,$key){ echo $key," "; if (is_array($item)) { echo array_keys($item)[1],"\n"; $popped = array_pop($item); foreach ($popped as $key => $arr) { usort($arr, 'sort_by_order'); echo "Median ($key): ",calculate_median( $arr ),"\n"; } } } array_walk($a, 'test');
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iHeuv
function name:  (null)
number of ops:  20
compiled vars:  !0 = $a
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   20     0  E >   ASSIGN                                                       !0, <array>
   21     1        ASSIGN_DIM                                                   !0, '2017-05-01'
          2        OP_DATA                                                      <array>
   23     3        FETCH_DIM_W                                          $3      !0, '2017-05-01'
          4        FETCH_DIM_W                                          $4      $3, 'DC'
          5        ASSIGN_DIM                                                   $4, 'IT'
          6        OP_DATA                                                      <array>
   24     7        FETCH_DIM_W                                          $6      !0, '2017-05-01'
          8        FETCH_DIM_W                                          $7      $6, 'DC'
          9        ASSIGN_DIM                                                   $7, 'DE'
         10        OP_DATA                                                      <array>
   25    11        FETCH_DIM_W                                          $9      !0, '2017-05-01'
         12        FETCH_DIM_W                                          $10     $9, 'DC'
         13        ASSIGN_DIM                                                   $10, 'Other'
         14        OP_DATA                                                      <array>
   47    15        INIT_FCALL                                                   'array_walk'
         16        SEND_REF                                                     !0
         17        SEND_VAL                                                     'test'
         18        DO_ICALL                                                     
         19      > RETURN                                                       1

Function calculate_median:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iHeuv
function name:  calculate_median
number of ops:  27
compiled vars:  !0 = $arr, !1 = $count, !2 = $middleval, !3 = $median, !4 = $low, !5 = $high
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    5     0  E >   RECV                                                 !0      
    6     1        INIT_FCALL                                                   'sort'
          2        SEND_REF                                                     !0
          3        DO_ICALL                                                     
    7     4        COUNT                                                ~7      !0
          5        ASSIGN                                                       !1, ~7
    8     6        INIT_FCALL                                                   'floor'
          7        SUB                                                  ~9      !1, 1
          8        DIV                                                  ~10     ~9, 2
          9        SEND_VAL                                                     ~10
         10        DO_ICALL                                             $11     
         11        ASSIGN                                                       !2, $11
    9    12        MOD                                                  ~13     !1, 2
         13      > JMPZ                                                         ~13, ->17
   10    14    >   FETCH_DIM_R                                          ~14     !0, !2
         15        ASSIGN                                                       !3, ~14
    9    16      > JMP                                                          ->25
   12    17    >   FETCH_DIM_R                                          ~16     !0, !2
         18        ASSIGN                                                       !4, ~16
   13    19        ADD                                                  ~18     !2, 1
         20        FETCH_DIM_R                                          ~19     !0, ~18
         21        ASSIGN                                                       !5, ~19
   14    22        ADD                                                  ~21     !4, !5
         23        DIV                                                  ~22     ~21, 2
         24        ASSIGN                                                       !3, ~22
   16    25    > > RETURN                                                       !3
   17    26*     > RETURN                                                       null

End of function calculate_median

Function sort_by_order:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 8
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
Branch analysis from position: 5
filename:       /in/iHeuv
function name:  sort_by_order
number of ops:  11
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   30     2        IS_EQUAL                                                     !0, ''
          3      > JMPZ                                                         ~2, ->5
          4    >   ASSIGN                                                       !0, 0
   31     5    >   IS_EQUAL                                                     !1, ''
          6      > JMPZ                                                         ~4, ->8
          7    >   ASSIGN                                                       !1, 0
   32     8    >   SUB                                                  ~6      !0, !1
          9      > RETURN                                                       ~6
   33    10*     > RETURN                                                       null

End of function sort_by_order

Function test:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 34
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 33
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 33
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
Branch analysis from position: 34
filename:       /in/iHeuv
function name:  test
number of ops:  35
compiled vars:  !0 = $item, !1 = $key, !2 = $popped, !3 = $arr
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   35     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   36     2        ECHO                                                         !1
          3        ECHO                                                         '+'
   37     4        TYPE_CHECK                                      128          !0
          5      > JMPZ                                                         ~4, ->34
   38     6    >   INIT_FCALL                                                   'array_keys'
          7        SEND_VAR                                                     !0
          8        DO_ICALL                                             $5      
          9        FETCH_DIM_R                                          ~6      $5, 1
         10        ECHO                                                         ~6
         11        ECHO                                                         '%0A'
   39    12        INIT_FCALL                                                   'array_pop'
         13        SEND_REF                                                     !0
         14        DO_ICALL                                             $7      
         15        ASSIGN                                                       !2, $7
   40    16      > FE_RESET_R                                           $9      !2, ->33
         17    > > FE_FETCH_R                                           ~10     $9, !3, ->33
         18    >   ASSIGN                                                       !1, ~10
   41    19        INIT_FCALL                                                   'usort'
         20        SEND_REF                                                     !3
         21        SEND_VAL                                                     'sort_by_order'
         22        DO_ICALL                                                     
   42    23        ROPE_INIT                                         3  ~14     'Median+%28'
         24        ROPE_ADD                                          1  ~14     ~14, !1
         25        ROPE_END                                          2  ~13     ~14, '%29%3A+'
         26        ECHO                                                         ~13
         27        INIT_FCALL                                                   'calculate_median'
         28        SEND_VAR                                                     !3
         29        DO_FCALL                                          0  $16     
         30        ECHO                                                         $16
         31        ECHO                                                         '%0A'
   40    32      > JMP                                                          ->17
         33    >   FE_FREE                                                      $9
   45    34    > > RETURN                                                       null

End of function test

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
160.92 ms | 2610 KiB | 20 Q