3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dates = array("2012-06-11 08:30:49", "2012-06-07 08:03:54", "2012-05-26 23:04:04", "2012-05-27 08:30:00", "2012-06-08 08:30:55", "2012-06-12 07:45:45"); $children = array("callum" => "1028", "daniel" => "1029"); $closeDate = array(); $firstDate = strtotime("2012-05-29 07:45:45"); function returnClosestDate($firstTime, $dates, $child){ global $initialLoop; global $closeDate; foreach ($dates as $key => $date) { $dateToCompare = strtotime($date); $diff = $firstTime - $dateToCompare; if ($diff < 0) $diff *= -1; if (count($closeDate) == 0) { $closeDate[$key] = array("child" => $child, "date" => $date, "diff" => $diff); continue; } for ($x = 0; $x <= count($closeDate); $x++) { if ($diff < $closeDate[$x]["diff"]) { $closeDate[$x] = array("child" => $child, "date" => $date, "diff" => $diff); echo "<pre>"; print_r($closeDate); echo "</pre>"; } } exit; //if(!alreadyExistInArray($closeDate, $date, $child)){ //if ($diff < $closeDate[$key-1]["diff"]) { //$closeDate[$key] = array("child" => $child, "date" => $date, "diff" => $diff); // } //} } } function alreadyExistInArray($closeDate, $date, $child){ foreach($closeDate as $key => $close){ if($close["child"] === $child) return true; } } foreach($children as $key => $child){ returnClosestDate($firstDate, $dates, $child); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/7stnH
function name:  (null)
number of ops:  18
compiled vars:  !0 = $dates, !1 = $children, !2 = $closeDate, !3 = $firstDate, !4 = $child, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
    5     1        ASSIGN                                                   !1, <array>
    6     2        ASSIGN                                                   !2, <array>
    7     3        INIT_FCALL                                               'strtotime'
          4        SEND_VAL                                                 '2012-05-29+07%3A45%3A45'
          5        DO_ICALL                                         $9      
          6        ASSIGN                                                   !3, $9
   48     7      > FE_RESET_R                                       $11     !1, ->16
          8    > > FE_FETCH_R                                       ~12     $11, !4, ->16
          9    >   ASSIGN                                                   !5, ~12
   49    10        INIT_FCALL                                               'returnclosestdate'
         11        SEND_VAR                                                 !3
         12        SEND_VAR                                                 !0
         13        SEND_VAR                                                 !4
         14        DO_FCALL                                      0          
   48    15      > JMP                                                      ->8
         16    >   FE_FREE                                                  $11
   50    17      > RETURN                                                   1

Function returnclosestdate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 48
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 48
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 17
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 26
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
2 jumps found. (Code = 44) Position 1 = 46, Position 2 = 28
Branch analysis from position: 46
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 42
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 46, Position 2 = 28
Branch analysis from position: 46
Branch analysis from position: 28
Branch analysis from position: 42
Branch analysis from position: 17
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
filename:       /in/7stnH
function name:  returnClosestDate
number of ops:  50
compiled vars:  !0 = $firstTime, !1 = $dates, !2 = $child, !3 = $initialLoop, !4 = $closeDate, !5 = $date, !6 = $key, !7 = $dateToCompare, !8 = $diff, !9 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   10     3        BIND_GLOBAL                                              !3, 'initialLoop'
   11     4        BIND_GLOBAL                                              !4, 'closeDate'
   12     5      > FE_RESET_R                                       $10     !1, ->48
          6    > > FE_FETCH_R                                       ~11     $10, !5, ->48
          7    >   ASSIGN                                                   !6, ~11
   14     8        INIT_FCALL                                               'strtotime'
          9        SEND_VAR                                                 !5
         10        DO_ICALL                                         $13     
         11        ASSIGN                                                   !7, $13
   15    12        SUB                                              ~15     !0, !7
         13        ASSIGN                                                   !8, ~15
   16    14        IS_SMALLER                                               !8, 0
         15      > JMPZ                                                     ~17, ->17
         16    >   ASSIGN_OP                                     3          !8, -1
   18    17    >   COUNT                                            ~19     !4
         18        IS_EQUAL                                                 ~19, 0
         19      > JMPZ                                                     ~20, ->26
   19    20    >   INIT_ARRAY                                       ~22     !2, 'child'
         21        ADD_ARRAY_ELEMENT                                ~22     !5, 'date'
         22        ADD_ARRAY_ELEMENT                                ~22     !8, 'diff'
         23        ASSIGN_DIM                                               !4, !6
         24        OP_DATA                                                  ~22
   20    25      > JMP                                                      ->6
   23    26    >   ASSIGN                                                   !9, 0
         27      > JMP                                                      ->43
   24    28    >   FETCH_DIM_R                                      ~24     !4, !9
         29        FETCH_DIM_R                                      ~25     ~24, 'diff'
         30        IS_SMALLER                                               !8, ~25
         31      > JMPZ                                                     ~26, ->42
   25    32    >   INIT_ARRAY                                       ~28     !2, 'child'
         33        ADD_ARRAY_ELEMENT                                ~28     !5, 'date'
         34        ADD_ARRAY_ELEMENT                                ~28     !8, 'diff'
         35        ASSIGN_DIM                                               !4, !9
         36        OP_DATA                                                  ~28
   26    37        ECHO                                                     '%3Cpre%3E'
   27    38        INIT_FCALL                                               'print_r'
         39        SEND_VAR                                                 !4
         40        DO_ICALL                                                 
   28    41        ECHO                                                     '%3C%2Fpre%3E'
   23    42    >   PRE_INC                                                  !9
         43    >   COUNT                                            ~31     !4
         44        IS_SMALLER_OR_EQUAL                                      !9, ~31
         45      > JMPNZ                                                    ~32, ->28
   32    46    > > EXIT                                                     
   12    47*       JMP                                                      ->6
         48    >   FE_FREE                                                  $10
   40    49      > RETURN                                                   null

End of function returnclosestdate

Function alreadyexistinarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/7stnH
function name:  alreadyExistInArray
number of ops:  14
compiled vars:  !0 = $closeDate, !1 = $date, !2 = $child, !3 = $close, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   43     3      > FE_RESET_R                                       $5      !0, ->12
          4    > > FE_FETCH_R                                       ~6      $5, !3, ->12
          5    >   ASSIGN                                                   !4, ~6
   44     6        FETCH_DIM_R                                      ~8      !3, 'child'
          7        IS_IDENTICAL                                             !2, ~8
          8      > JMPZ                                                     ~9, ->11
          9    >   FE_FREE                                                  $5
         10      > RETURN                                                   <true>
   43    11    > > JMP                                                      ->4
         12    >   FE_FREE                                                  $5
   46    13      > RETURN                                                   null

End of function alreadyexistinarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.82 ms | 1407 KiB | 18 Q