3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sortAvg($array){ $a = array_slice($array, 0); $avg = array_sum($a)/count($a); echo $avg . "\n"; uasort($a, function($o, $b) use($avg){ return abs($o-$avg) > abs($b-$avg); }); return $a; } $array = [ 'A' => 50, 'B' => 155, 'C' => 75, 'D' => 245, 'E' => 300, 'F' => 100, 'G' => 153, ]; $results = sortAvg($array); $closestKey = array_keys($results)[0]; $closestValue = $results[$closestKey]; var_export([$closestKey, $closestValue]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lhmfg
function name:  (null)
number of ops:  18
compiled vars:  !0 = $array, !1 = $results, !2 = $closestKey, !3 = $closestValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   ASSIGN                                                   !0, <array>
   22     1        INIT_FCALL                                               'sortavg'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $5      
          4        ASSIGN                                                   !1, $5
   23     5        INIT_FCALL                                               'array_keys'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $7      
          8        FETCH_DIM_R                                      ~8      $7, 0
          9        ASSIGN                                                   !2, ~8
   24    10        FETCH_DIM_R                                      ~10     !1, !2
         11        ASSIGN                                                   !3, ~10
   25    12        INIT_FCALL                                               'var_export'
         13        INIT_ARRAY                                       ~12     !2
         14        ADD_ARRAY_ELEMENT                                ~12     !3
         15        SEND_VAL                                                 ~12
         16        DO_ICALL                                                 
         17      > RETURN                                                   1

Function sortavg:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lhmfg
function name:  sortAvg
number of ops:  22
compiled vars:  !0 = $array, !1 = $a, !2 = $avg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        INIT_FCALL                                               'array_slice'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 0
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !1, $3
    5     6        INIT_FCALL                                               'array_sum'
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                         $5      
          9        COUNT                                            ~6      !1
         10        DIV                                              ~7      $5, ~6
         11        ASSIGN                                                   !2, ~7
    6    12        CONCAT                                           ~9      !2, '%0A'
         13        ECHO                                                     ~9
    7    14        INIT_FCALL                                               'uasort'
         15        SEND_REF                                                 !1
         16        DECLARE_LAMBDA_FUNCTION                          ~10     [0]
         17        BIND_LEXICAL                                             ~10, !2
    9    18        SEND_VAL                                                 ~10
    7    19        DO_ICALL                                                 
   10    20      > RETURN                                                   !1
   11    21*     > 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/lhmfg
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $o, !1 = $b, !2 = $avg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
    8     3        INIT_FCALL                                               'abs'
          4        SUB                                              ~3      !0, !2
          5        SEND_VAL                                                 ~3
          6        DO_ICALL                                         $4      
          7        INIT_FCALL                                               'abs'
          8        SUB                                              ~5      !1, !2
          9        SEND_VAL                                                 ~5
         10        DO_ICALL                                         $6      
         11        IS_SMALLER                                       ~7      $6, $4
         12      > RETURN                                                   ~7
    9    13*     > RETURN                                                   null

End of Dynamic Function 0

End of function sortavg

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.75 ms | 1023 KiB | 20 Q