3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dates = [ ['date_starts'=>'2021-03-22'], ['date_starts'=>'2022-04-22'], ['date_starts'=>'2023-05-22'] ]; $current_year = date('Y'); foreach ($dates as $dt) { $rates_results = [ ['price'=>255, 'year'=>'2021'], ['price'=>200, 'year'=>'2021'], ['price'=>300, 'year'=>'2023'] ]; // get the year from the start date. Since it's in YYYY-MM-DD format // we can just use substr $start_year = substr($dt['date_starts'], 0, 4); // find matching years in $rates_results $rate_keys = array_keys(array_column($rates_results, 'year'), $start_year); // any matches? if (empty($rate_keys)) { // no, use the values from the current year instead $rate_keys = array_keys(array_column($rates_results, 'year'), $current_year); } // get the actual rates $code = 1; $rates = array(); foreach ($rate_keys as $key) { $rates[] = [ 'custom' => $code, 'price' => $rates_results[$key]['price'], 'year' => $rates_results[$key]['year'] ]; } // output the rates print_r($rates); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 54
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 54
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 35
Branch analysis from position: 26
2 jumps found. (Code = 77) Position 1 = 38, Position 2 = 49
Branch analysis from position: 38
2 jumps found. (Code = 78) Position 1 = 39, Position 2 = 49
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 49
Branch analysis from position: 35
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 54
filename:       /in/9HZL8
function name:  (null)
number of ops:  56
compiled vars:  !0 = $dates, !1 = $current_year, !2 = $dt, !3 = $rates_results, !4 = $start_year, !5 = $rate_keys, !6 = $code, !7 = $rates, !8 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    9     1        INIT_FCALL                                               'date'
          2        SEND_VAL                                                 'Y'
          3        DO_ICALL                                         $10     
          4        ASSIGN                                                   !1, $10
   11     5      > FE_RESET_R                                       $12     !0, ->54
          6    > > FE_FETCH_R                                               $12, !2, ->54
   13     7    >   ASSIGN                                                   !3, <array>
   21     8        INIT_FCALL                                               'substr'
          9        FETCH_DIM_R                                      ~14     !2, 'date_starts'
         10        SEND_VAL                                                 ~14
         11        SEND_VAL                                                 0
         12        SEND_VAL                                                 4
         13        DO_ICALL                                         $15     
         14        ASSIGN                                                   !4, $15
   24    15        INIT_FCALL                                               'array_keys'
         16        INIT_FCALL                                               'array_column'
         17        SEND_VAR                                                 !3
         18        SEND_VAL                                                 'year'
         19        DO_ICALL                                         $17     
         20        SEND_VAR                                                 $17
         21        SEND_VAR                                                 !4
         22        DO_ICALL                                         $18     
         23        ASSIGN                                                   !5, $18
   27    24        ISSET_ISEMPTY_CV                                         !5
         25      > JMPZ                                                     ~20, ->35
   29    26    >   INIT_FCALL                                               'array_keys'
         27        INIT_FCALL                                               'array_column'
         28        SEND_VAR                                                 !3
         29        SEND_VAL                                                 'year'
         30        DO_ICALL                                         $21     
         31        SEND_VAR                                                 $21
         32        SEND_VAR                                                 !1
         33        DO_ICALL                                         $22     
         34        ASSIGN                                                   !5, $22
   33    35    >   ASSIGN                                                   !6, 1
   34    36        ASSIGN                                                   !7, <array>
   35    37      > FE_RESET_R                                       $26     !5, ->49
         38    > > FE_FETCH_R                                               $26, !8, ->49
   37    39    >   INIT_ARRAY                                       ~28     !6, 'custom'
   38    40        FETCH_DIM_R                                      ~29     !3, !8
         41        FETCH_DIM_R                                      ~30     ~29, 'price'
         42        ADD_ARRAY_ELEMENT                                ~28     ~30, 'price'
   39    43        FETCH_DIM_R                                      ~31     !3, !8
         44        FETCH_DIM_R                                      ~32     ~31, 'year'
         45        ADD_ARRAY_ELEMENT                                ~28     ~32, 'year'
   36    46        ASSIGN_DIM                                               !7
   39    47        OP_DATA                                                  ~28
   35    48      > JMP                                                      ->38
         49    >   FE_FREE                                                  $26
   44    50        INIT_FCALL                                               'print_r'
         51        SEND_VAR                                                 !7
         52        DO_ICALL                                                 
   11    53      > JMP                                                      ->6
         54    >   FE_FREE                                                  $12
   45    55      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
137.52 ms | 1007 KiB | 18 Q