3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr = array('05:11','05:21','05:24','05:31','05:34','05:41','05:44','05:50','05:54'); function getClosest($search, $arr) { $item = null; while ($key = key($arr) !== null) { $current = current($arr); $item = next($arr); if ( strtotime($current) < strtotime($search) && strtotime($item) >= strtotime($search) ) { break; } else if ( strtotime($current) > strtotime($search) ) { $item = $current; break; } } return $item; } print_r([ getClosest('05:50', $arr), getClosest('05:34', $arr), getClosest('05:52', $arr), getClosest('05:15', $arr), getClosest('05:10', $arr), ]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tqHOC
function name:  (null)
number of ops:  30
compiled vars:  !0 = $arr
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                       !0, <array>
   25     1        INIT_FCALL                                                   'print_r'
   26     2        INIT_FCALL                                                   'getclosest'
          3        SEND_VAL                                                     '05%3A50'
          4        SEND_VAR                                                     !0
          5        DO_FCALL                                          0  $2      
          6        INIT_ARRAY                                           ~3      $2
   27     7        INIT_FCALL                                                   'getclosest'
          8        SEND_VAL                                                     '05%3A34'
          9        SEND_VAR                                                     !0
         10        DO_FCALL                                          0  $4      
         11        ADD_ARRAY_ELEMENT                                    ~3      $4
   28    12        INIT_FCALL                                                   'getclosest'
         13        SEND_VAL                                                     '05%3A52'
         14        SEND_VAR                                                     !0
         15        DO_FCALL                                          0  $5      
         16        ADD_ARRAY_ELEMENT                                    ~3      $5
   29    17        INIT_FCALL                                                   'getclosest'
         18        SEND_VAL                                                     '05%3A15'
         19        SEND_VAR                                                     !0
         20        DO_FCALL                                          0  $6      
         21        ADD_ARRAY_ELEMENT                                    ~3      $6
   30    22        INIT_FCALL                                                   'getclosest'
         23        SEND_VAL                                                     '05%3A10'
         24        SEND_VAR                                                     !0
         25        DO_FCALL                                          0  $7      
         26        ADD_ARRAY_ELEMENT                                    ~3      $7
         27        SEND_VAL                                                     ~3
   25    28        DO_ICALL                                                     
   31    29      > RETURN                                                       1

Function getclosest:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 4
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 20, Position 2 = 28
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 41
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 41
Branch analysis from position: 28
filename:       /in/tqHOC
function name:  getClosest
number of ops:  49
compiled vars:  !0 = $search, !1 = $arr, !2 = $item, !3 = $current, !4 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
    5     2        ASSIGN                                                       !2, null
    6     3      > JMP                                                          ->41
    7     4    >   INIT_FCALL                                                   'current'
          5        SEND_VAR                                                     !1
          6        DO_ICALL                                             $6      
          7        ASSIGN                                                       !3, $6
    8     8        INIT_FCALL                                                   'next'
          9        SEND_REF                                                     !1
         10        DO_ICALL                                             $8      
         11        ASSIGN                                                       !2, $8
   10    12        INIT_FCALL                                                   'strtotime'
         13        SEND_VAR                                                     !3
         14        DO_ICALL                                             $10     
         15        INIT_FCALL                                                   'strtotime'
         16        SEND_VAR                                                     !0
         17        DO_ICALL                                             $11     
         18        IS_SMALLER                                           ~12     $10, $11
         19      > JMPZ_EX                                              ~12     ~12, ->28
   11    20    >   INIT_FCALL                                                   'strtotime'
         21        SEND_VAR                                                     !2
         22        DO_ICALL                                             $13     
         23        INIT_FCALL                                                   'strtotime'
         24        SEND_VAR                                                     !0
         25        DO_ICALL                                             $14     
         26        IS_SMALLER_OR_EQUAL                                  ~15     $14, $13
         27        BOOL                                                 ~12     ~15
         28    > > JMPZ                                                         ~12, ->31
   13    29    > > JMP                                                          ->47
   10    30*       JMP                                                          ->41
   15    31    >   INIT_FCALL                                                   'strtotime'
         32        SEND_VAR                                                     !3
         33        DO_ICALL                                             $16     
         34        INIT_FCALL                                                   'strtotime'
         35        SEND_VAR                                                     !0
         36        DO_ICALL                                             $17     
         37        IS_SMALLER                                                   $17, $16
         38      > JMPZ                                                         ~18, ->41
   17    39    >   ASSIGN                                                       !2, !3
   18    40      > JMP                                                          ->47
    6    41    >   INIT_FCALL                                                   'key'
         42        SEND_VAR                                                     !1
         43        DO_ICALL                                             $20     
         44        TYPE_CHECK                                      1020  ~21     $20
         45        ASSIGN                                               ~22     !4, ~21
         46      > JMPNZ                                                        ~22, ->4
   22    47    > > RETURN                                                       !2
   23    48*     > RETURN                                                       null

End of function getclosest

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
162.61 ms | 2087 KiB | 23 Q