3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dates[] = array("date" => "2016-02-18 02:00:00", "duration" => "600"); // 10 mins $dates[] = array("date" => "2016-02-18 02:05:00", "duration" => "300"); // 5 mins $dates[] = array("date" => "2016-02-18 02:10:00", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:25:00", "duration" => "300"); $dates[] = array("date" => "2016-02-18 02:30:00", "duration" => "600"); $children = array("callum" => "1028", "daniel" => "1029"); $closeDate[] = array("date" => "2016-02-18 02:00:00", "duration" => "600"); // 10 mins function returnClosestDate($child){ global $initialLoop; global $closeDate; global $dates; foreach ($dates as $key => $date) { $dateToCompare = strtotime($date); $diff = $firstTime - $dateToCompare; if ($diff < 0) $diff *= -1; if (count($closeDate) == 0) { $closeDate[][$child] = array("date" => $date, "diff" => $diff); continue; } for ($x = 0; $x <= count($closeDate); $x++) { if(isset($closeDate[$x])){ if ($diff < $closeDate[$x][$child]["diff"]) { $closeDate[$x][$child] = array("date" => $date, "diff" => $diff); } } } //if(!alreadyExistInArray($closeDate, $date, $child)){ //if ($diff < $closeDate[$key-1]["diff"]) { //$closeDate[$key] = array("child" => $child, "date" => $date, "diff" => $diff); // } //} } echo "<pre>"; print_r($closeDate); echo "</pre>"; exit; } function alreadyExistInArray($closeDate, $date, $child){ foreach($closeDate as $key => $close){ if($close["child"] === $child) return true; } } foreach($children as $key => $child){ returnClosestDate($child); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 20
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/mjvJs
function name:  (null)
number of ops:  22
compiled vars:  !0 = $dates, !1 = $children, !2 = $closeDate, !3 = $child, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN_DIM                                               !0
          1        OP_DATA                                                  <array>
    4     2        ASSIGN_DIM                                               !0
          3        OP_DATA                                                  <array>
    5     4        ASSIGN_DIM                                               !0
          5        OP_DATA                                                  <array>
    6     6        ASSIGN_DIM                                               !0
          7        OP_DATA                                                  <array>
    7     8        ASSIGN_DIM                                               !0
          9        OP_DATA                                                  <array>
    9    10        ASSIGN                                                   !1, <array>
   10    11        ASSIGN_DIM                                               !2
         12        OP_DATA                                                  <array>
   54    13      > FE_RESET_R                                       $12     !1, ->20
         14    > > FE_FETCH_R                                       ~13     $12, !3, ->20
         15    >   ASSIGN                                                   !4, ~13
   55    16        INIT_FCALL                                               'returnclosestdate'
         17        SEND_VAR                                                 !3
         18        DO_FCALL                                      0          
   54    19      > JMP                                                      ->14
         20    >   FE_FREE                                                  $12
   56    21      > RETURN                                                   1

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

End of function alreadyexistinarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.96 ms | 1407 KiB | 18 Q