3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array ( 0=>array( 'date'=>'2017-09-01', 'total'=>4 ), 1=>array( 'date'=>'2017-09-07', 'total'=>6 ), 2=>array( 'date'=>'2017-09-12', 'total'=>6 ), 3=>array( 'date'=>'2017-09-14', 'total'=>6 ) ); // array with only dates to search in $dates = array_column($arr, "date"); // Create an array with all dates from first item to last item $start = new DateTime($arr[0]["date"]); $end = new DateTime(end($arr)["date"]); $range = new DatePeriod($start, new DateInterval('P1D'), $end); // $range is now all dates from start to end minus last one. // Loop through the range foreach($range as $date){ //See if the current date exist is first array $find = array_search($date->format("Y-m-d"), $dates); If($find !== false){ $result[] = $arr[$find]; // if it does copy it to result array }Else{ // If not add it and create a total = 0 $result[] = array('date' => $date->format("Y-m-d"), 'total' => 0); } } // Since the loop does not loop all dates we need to add the last item to result. $result[] = end($arr); Var_dump($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 55
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 55
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 47
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 55
filename:       /in/Hf73Z
function name:  (null)
number of ops:  65
compiled vars:  !0 = $arr, !1 = $dates, !2 = $start, !3 = $end, !4 = $range, !5 = $date, !6 = $find, !7 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   22     1        INIT_FCALL                                               'array_column'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'date'
          4        DO_ICALL                                         $9      
          5        ASSIGN                                                   !1, $9
   25     6        NEW                                              $11     'DateTime'
          7        CHECK_FUNC_ARG                                           
          8        FETCH_DIM_FUNC_ARG                               $12     !0, 0
          9        FETCH_DIM_FUNC_ARG                               $13     $12, 'date'
         10        SEND_FUNC_ARG                                            $13
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !2, $11
   26    13        NEW                                              $16     'DateTime'
         14        CHECK_FUNC_ARG                                           
         15        INIT_FCALL                                               'end'
         16        SEND_REF                                                 !0
         17        DO_ICALL                                         $17     
         18        FETCH_DIM_FUNC_ARG                               $18     $17, 'date'
         19        SEND_FUNC_ARG                                            $18
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !3, $16
   28    22        NEW                                              $21     'DatePeriod'
         23        SEND_VAR_EX                                              !2
         24        NEW                                              $22     'DateInterval'
         25        SEND_VAL_EX                                              'P1D'
         26        DO_FCALL                                      0          
         27        SEND_VAR_NO_REF_EX                                       $22
         28        SEND_VAR_EX                                              !3
         29        DO_FCALL                                      0          
         30        ASSIGN                                                   !4, $21
   33    31      > FE_RESET_R                                       $26     !4, ->55
         32    > > FE_FETCH_R                                               $26, !5, ->55
   35    33    >   INIT_FCALL                                               'array_search'
         34        INIT_METHOD_CALL                                         !5, 'format'
         35        SEND_VAL_EX                                              'Y-m-d'
         36        DO_FCALL                                      0  $27     
         37        SEND_VAR                                                 $27
         38        SEND_VAR                                                 !1
         39        DO_ICALL                                         $28     
         40        ASSIGN                                                   !6, $28
   36    41        TYPE_CHECK                                  1018          !6
         42      > JMPZ                                                     ~30, ->47
   37    43    >   FETCH_DIM_R                                      ~32     !0, !6
         44        ASSIGN_DIM                                               !7
         45        OP_DATA                                                  ~32
   36    46      > JMP                                                      ->54
   40    47    >   INIT_METHOD_CALL                                         !5, 'format'
         48        SEND_VAL_EX                                              'Y-m-d'
         49        DO_FCALL                                      0  $34     
         50        INIT_ARRAY                                       ~35     $34, 'date'
         51        ADD_ARRAY_ELEMENT                                ~35     0, 'total'
         52        ASSIGN_DIM                                               !7
         53        OP_DATA                                                  ~35
   33    54    > > JMP                                                      ->32
         55    >   FE_FREE                                                  $26
   44    56        INIT_FCALL                                               'end'
         57        SEND_REF                                                 !0
         58        DO_ICALL                                         $37     
         59        ASSIGN_DIM                                               !7
         60        OP_DATA                                                  $37
   45    61        INIT_FCALL                                               'var_dump'
         62        SEND_VAR                                                 !7
         63        DO_ICALL                                                 
         64      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.05 ms | 1018 KiB | 17 Q