3v4l.org

run code in 500+ 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 = 51
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 51
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 32
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 35, Position 2 = 46
Branch analysis from position: 35
2 jumps found. (Code = 78) Position 1 = 36, Position 2 = 46
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 46
Branch analysis from position: 32
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
filename:       /in/9HZL8
function name:  (null)
number of ops:  53
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, ->51
          6    > > FE_FETCH_R                                                   $12, !2, ->51
   13     7    >   ASSIGN                                                       !3, <array>
   21     8        FETCH_DIM_R                                          ~14     !2, 'date_starts'
          9        FRAMELESS_ICALL_3                substr              ~15     ~14, 0
         10        OP_DATA                                                      4
         11        ASSIGN                                                       !4, ~15
   24    12        INIT_FCALL                                                   'array_keys'
         13        INIT_FCALL                                                   'array_column'
         14        SEND_VAR                                                     !3
         15        SEND_VAL                                                     'year'
         16        DO_ICALL                                             $17     
         17        SEND_VAR                                                     $17
         18        SEND_VAR                                                     !4
         19        DO_ICALL                                             $18     
         20        ASSIGN                                                       !5, $18
   27    21        ISSET_ISEMPTY_CV                                             !5
         22      > JMPZ                                                         ~20, ->32
   29    23    >   INIT_FCALL                                                   'array_keys'
         24        INIT_FCALL                                                   'array_column'
         25        SEND_VAR                                                     !3
         26        SEND_VAL                                                     'year'
         27        DO_ICALL                                             $21     
         28        SEND_VAR                                                     $21
         29        SEND_VAR                                                     !1
         30        DO_ICALL                                             $22     
         31        ASSIGN                                                       !5, $22
   33    32    >   ASSIGN                                                       !6, 1
   34    33        ASSIGN                                                       !7, <array>
   35    34      > FE_RESET_R                                           $26     !5, ->46
         35    > > FE_FETCH_R                                                   $26, !8, ->46
   37    36    >   INIT_ARRAY                                           ~28     !6, 'custom'
   38    37        FETCH_DIM_R                                          ~29     !3, !8
         38        FETCH_DIM_R                                          ~30     ~29, 'price'
         39        ADD_ARRAY_ELEMENT                                    ~28     ~30, 'price'
   39    40        FETCH_DIM_R                                          ~31     !3, !8
         41        FETCH_DIM_R                                          ~32     ~31, 'year'
         42        ADD_ARRAY_ELEMENT                                    ~28     ~32, 'year'
   36    43        ASSIGN_DIM                                                   !7
   39    44        OP_DATA                                                      ~28
   35    45      > JMP                                                          ->35
         46    >   FE_FREE                                                      $26
   44    47        INIT_FCALL                                                   'print_r'
         48        SEND_VAR                                                     !7
         49        DO_ICALL                                                     
   11    50      > JMP                                                          ->6
         51    >   FE_FREE                                                      $12
   45    52      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
160.06 ms | 2141 KiB | 17 Q