3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ ['threshold' => 3000, 'value' => 216], ['threshold' => 4000, 'value' => 254], ['threshold' => 5000, 'value' => 292], ['threshold' => 6000, 'value' => 330], ['threshold' => 7000, 'value' => 368], ['threshold' => 8000, 'value' => 406], ['threshold' => 9000, 'value' => 444], ['threshold' => 10000, 'value' => 482], ['threshold' => 13000, 'value' => 534], ['threshold' => 16000, 'value' => 586], ['threshold' => 19000, 'value' => 638], ['threshold' => 22000, 'value' => 690], ['threshold' => 25000, 'value' => 742], ['threshold' => 30000, 'value' => 812], ['threshold' => 35000, 'value' => 882], ['threshold' => 40000, 'value' => 952], ['threshold' => 45000, 'value' => 1022], ['threshold' => 50000, 'value' => 1092], ['threshold' => 65000, 'value' => 1332], ['threshold' => 80000, 'value' => 1572], ['threshold' => 95000, 'value' => 1812], ['threshold' => 110000, 'value' => 2052], ['threshold' => 125000, 'value' => 2292], ['threshold' => 140000, 'value' => 2532], ['threshold' => 155000, 'value' => 2772], ['threshold' => 170000, 'value' => 3012], ['threshold' => 185000, 'value' => 3252], ['threshold' => 200000, 'value' => 3492], ['threshold' => 230000, 'value' => 3850], ['threshold' => 260000, 'value' => 4208], ['threshold' => 290000, 'value' => 4566], ['threshold' => 320000, 'value' => 4924], ['threshold' => 350000, 'value' => 5282], ['threshold' => 380000, 'value' => 5640], ['threshold' => 410000, 'value' => 5998], ['threshold' => 440000, 'value' => 6356], ['threshold' => 470000, 'value' => 6714], ['threshold' => 500000, 'value' => 7072], ['threshold' => 550000, 'value' => 7432], ['threshold' => 600000, 'value' => 7792], ['threshold' => 650000, 'value' => 8152], ['threshold' => 700000, 'value' => 8512], ['threshold' => 750000, 'value' => 8872], ['threshold' => 800000, 'value' => 9232], ['threshold' => 850000, 'value' => 9592], ['threshold' => 900000, 'value' => 9952], ['threshold' => 950000, 'value' => 10312], ['threshold' => 1000000, 'value' => 10672] ]; function getClosest($search, $arr) { $closest = null; $i = 0; $index = 0; // Loop over the array and check if the currently closest value is higher than the current iteration foreach ($arr as $item) { if ($closest === null || abs($search - $closest['threshold']) > abs($item['threshold'] - $search)) { $closest = $item; $index = $i; } $i++; } $finalIndex = $index + 1; if($finalIndex >= count($arr)) { $finalIndex -= 1; } return floatval($arr[$finalIndex]['value']); } $result = getClosest(2500, $arr); echo $result;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OBKI4
function name:  (null)
number of ops:  8
compiled vars:  !0 = $arr, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   76     1        INIT_FCALL                                               'getclosest'
          2        SEND_VAL                                                 2500
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                                   !1, $3
   78     6        ECHO                                                     !1
          7      > RETURN                                                   1

Function getclosest:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 26
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 26
Branch analysis from position: 7
2 jumps found. (Code = 47) Position 1 = 9, Position 2 = 21
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 24
Branch analysis from position: 21
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 33
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
Branch analysis from position: 26
filename:       /in/OBKI4
function name:  getClosest
number of ops:  38
compiled vars:  !0 = $search, !1 = $arr, !2 = $closest, !3 = $i, !4 = $index, !5 = $item, !6 = $finalIndex
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   55     2        ASSIGN                                                   !2, null
   56     3        ASSIGN                                                   !3, 0
   57     4        ASSIGN                                                   !4, 0
   60     5      > FE_RESET_R                                       $10     !1, ->26
          6    > > FE_FETCH_R                                               $10, !5, ->26
   61     7    >   TYPE_CHECK                                    2  ~11     !2
          8      > JMPNZ_EX                                         ~11     ~11, ->21
          9    >   INIT_FCALL                                               'abs'
         10        FETCH_DIM_R                                      ~12     !2, 'threshold'
         11        SUB                                              ~13     !0, ~12
         12        SEND_VAL                                                 ~13
         13        DO_ICALL                                         $14     
         14        INIT_FCALL                                               'abs'
         15        FETCH_DIM_R                                      ~15     !5, 'threshold'
         16        SUB                                              ~16     ~15, !0
         17        SEND_VAL                                                 ~16
         18        DO_ICALL                                         $17     
         19        IS_SMALLER                                       ~18     $17, $14
         20        BOOL                                             ~11     ~18
         21    > > JMPZ                                                     ~11, ->24
   62    22    >   ASSIGN                                                   !2, !5
   63    23        ASSIGN                                                   !4, !3
   65    24    >   PRE_INC                                                  !3
   60    25      > JMP                                                      ->6
         26    >   FE_FREE                                                  $10
   68    27        ADD                                              ~22     !4, 1
         28        ASSIGN                                                   !6, ~22
   69    29        COUNT                                            ~24     !1
         30        IS_SMALLER_OR_EQUAL                                      ~24, !6
         31      > JMPZ                                                     ~25, ->33
   70    32    >   ASSIGN_OP                                     2          !6, 1
   73    33    >   FETCH_DIM_R                                      ~27     !1, !6
         34        FETCH_DIM_R                                      ~28     ~27, 'value'
         35        CAST                                          5  ~29     ~28
         36      > RETURN                                                   ~29
   74    37*     > RETURN                                                   null

End of function getclosest

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.69 ms | 1407 KiB | 16 Q