3v4l.org

run code in 300+ 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=[ '2017-05-01'=>[ 'DC'=>[ 'IT'=>[90,0], 'DE'=>[18,315,40,"",69,211], 'Other'=>[107,46,"","",27,22] ] ], '2017-05-02'=>[ 'DC'=>[ 'IT'=>[70,40,55], 'DE'=>["",31,4,"",9], 'Other'=>[1107,12,0,20,1,11,21] ] ] ]; function sort_by_order ($a, $b) { if ($a == "") $a = 0; if ($b == "") $b = 0; return $a - $b; } function test($item,$key){ echo "\n",$key," "; if (is_array($item)) { echo array_keys($item)[0],"\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/UC1o0
function name:  (null)
number of ops:  6
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   ASSIGN                                                   !0, <array>
   59     1        INIT_FCALL                                               'array_walk'
          2        SEND_REF                                                 !0
          3        SEND_VAL                                                 'test'
          4        DO_ICALL                                                 
          5      > 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/UC1o0
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/UC1o0
function name:  sort_by_order
number of ops:  11
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   42     2        IS_EQUAL                                                 !0, ''
          3      > JMPZ                                                     ~2, ->5
          4    >   ASSIGN                                                   !0, 0
   43     5    >   IS_EQUAL                                                 !1, ''
          6      > JMPZ                                                     ~4, ->8
          7    >   ASSIGN                                                   !1, 0
   44     8    >   SUB                                              ~6      !0, !1
          9      > RETURN                                                   ~6
   45    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 = 7, Position 2 = 35
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 34
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 34
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
Branch analysis from position: 35
filename:       /in/UC1o0
function name:  test
number of ops:  36
compiled vars:  !0 = $item, !1 = $key, !2 = $popped, !3 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   48     2        ECHO                                                     '%0A'
          3        ECHO                                                     !1
          4        ECHO                                                     '+'
   49     5        TYPE_CHECK                                  128          !0
          6      > JMPZ                                                     ~4, ->35
   50     7    >   INIT_FCALL                                               'array_keys'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $5      
         10        FETCH_DIM_R                                      ~6      $5, 0
         11        ECHO                                                     ~6
         12        ECHO                                                     '%0A'
   51    13        INIT_FCALL                                               'array_pop'
         14        SEND_REF                                                 !0
         15        DO_ICALL                                         $7      
         16        ASSIGN                                                   !2, $7
   52    17      > FE_RESET_R                                       $9      !2, ->34
         18    > > FE_FETCH_R                                       ~10     $9, !3, ->34
         19    >   ASSIGN                                                   !1, ~10
   53    20        INIT_FCALL                                               'usort'
         21        SEND_REF                                                 !3
         22        SEND_VAL                                                 'sort_by_order'
         23        DO_ICALL                                                 
   54    24        ROPE_INIT                                     3  ~14     'Median+%28'
         25        ROPE_ADD                                      1  ~14     ~14, !1
         26        ROPE_END                                      2  ~13     ~14, '%29%3A+'
         27        ECHO                                                     ~13
         28        INIT_FCALL                                               'calculate_median'
         29        SEND_VAR                                                 !3
         30        DO_FCALL                                      0  $16     
         31        ECHO                                                     $16
         32        ECHO                                                     '%0A'
   52    33      > JMP                                                      ->18
         34    >   FE_FREE                                                  $9
   57    35    > > RETURN                                                   null

End of function test

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.18 ms | 1019 KiB | 20 Q