3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array1= [ [ "month1" => "January", "2020cs" => 84, "2020as" => 500, "2019cs" => 17, "2019as" => 500, ], [ "month1" => "February", "2020cs" => 54, "2020as" => 200, "2019cs" => 12, "2019as" => 1000, ], [ "month1" => "April", "2020cs" => 4, "2020as" => 100, "2019cs" => 12, "2019as" => 1400, ], [ "month1" => "November", "2020cs" => 0, "2020as" => 0, "2019cs" => 7, "2019as" => 200, ], ]; $array2= [ [ "month1" => "January", "2020cr" => 13, "2020ar" => 300, "2019cr" => 0, "2019ar" => 0, ], [ "month1" => "March", "2020cr" => 1, "2020ar" => 100, "2019cr" => 0, "2019ar" => 0, ], [ "month1" => "November", "2020cr" => 0, "2020ar" => 0, "2019cr" => 1, "2019ar" => 800, ], [ "month1" => "December", "2020cr" => 0, "2020ar" => 0, "2019cr" => 2, "2019ar" => 500, ], ]; // top-level array from indexed to associative (name of month) $array1 = rekey($array1); $array2 = rekey($array2); function rekey(array $arr = []): array { foreach ($arr as $key => $record) { $arr[$record['month1']] = $arr[$key]; // indexed to name of month unset($arr[$key]); } return $arr; } // run the merge $result = array_merge_recursive($array1, $array2); // clean up double entries key 'month1' foreach($result as $key => &$value) { if(is_array($value['month1'])) $value['month1'] = $value['month1'][0]; } // sort result set $months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; foreach($result as $key0 => $value0) { foreach($months as $key1 => $value1) { if($key0 === $value1) { $result[$key1] = $result[$key0]; // set index based on month 0-11 unset($result[$key0]); } } } ksort($result); print_r(array_values($result));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 16, Position 2 = 26
Branch analysis from position: 16
2 jumps found. (Code = 126) Position 1 = 17, Position 2 = 26
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 25
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 25
Branch analysis from position: 26
2 jumps found. (Code = 77) Position 1 = 29, Position 2 = 43
Branch analysis from position: 29
2 jumps found. (Code = 78) Position 1 = 30, Position 2 = 43
Branch analysis from position: 30
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 41
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 41
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 40
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 40
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 41
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
Branch analysis from position: 26
filename:       /in/4ok0u
function name:  (null)
number of ops:  54
compiled vars:  !0 = $array1, !1 = $array2, !2 = $result, !3 = $value, !4 = $key, !5 = $months, !6 = $value0, !7 = $key0, !8 = $value1, !9 = $key1
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   34     1        ASSIGN                                                   !1, <array>
   65     2        INIT_FCALL_BY_NAME                                       'rekey'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $12     
          5        ASSIGN                                                   !0, $12
   66     6        INIT_FCALL_BY_NAME                                       'rekey'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $14     
          9        ASSIGN                                                   !1, $14
   77    10        INIT_FCALL                                               'array_merge_recursive'
         11        SEND_VAR                                                 !0
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $16     
         14        ASSIGN                                                   !2, $16
   80    15      > FE_RESET_RW                                      $18     !2, ->26
         16    > > FE_FETCH_RW                                      ~19     $18, !3, ->26
         17    >   ASSIGN                                                   !4, ~19
   81    18        FETCH_DIM_R                                      ~21     !3, 'month1'
         19        TYPE_CHECK                                  128          ~21
         20      > JMPZ                                                     ~22, ->25
         21    >   FETCH_DIM_R                                      ~24     !3, 'month1'
         22        FETCH_DIM_R                                      ~25     ~24, 0
         23        ASSIGN_DIM                                               !3, 'month1'
         24        OP_DATA                                                  ~25
   80    25    > > JMP                                                      ->16
         26    >   FE_FREE                                                  $18
   85    27        ASSIGN                                                   !5, <array>
   86    28      > FE_RESET_R                                       $27     !2, ->43
         29    > > FE_FETCH_R                                       ~28     $27, !6, ->43
         30    >   ASSIGN                                                   !7, ~28
   87    31      > FE_RESET_R                                       $30     !5, ->41
         32    > > FE_FETCH_R                                       ~31     $30, !8, ->41
         33    >   ASSIGN                                                   !9, ~31
   88    34        IS_IDENTICAL                                             !7, !8
         35      > JMPZ                                                     ~33, ->40
   89    36    >   FETCH_DIM_R                                      ~35     !2, !7
         37        ASSIGN_DIM                                               !2, !9
         38        OP_DATA                                                  ~35
   90    39        UNSET_DIM                                                !2, !7
   87    40    > > JMP                                                      ->32
         41    >   FE_FREE                                                  $30
   86    42      > JMP                                                      ->29
         43    >   FE_FREE                                                  $27
   94    44        INIT_FCALL                                               'ksort'
         45        SEND_REF                                                 !2
         46        DO_ICALL                                                 
   96    47        INIT_FCALL                                               'print_r'
         48        INIT_FCALL                                               'array_values'
         49        SEND_VAR                                                 !2
         50        DO_ICALL                                         $37     
         51        SEND_VAR                                                 $37
         52        DO_ICALL                                                 
         53      > RETURN                                                   1

Function rekey:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 10
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/4ok0u
function name:  rekey
number of ops:  15
compiled vars:  !0 = $arr, !1 = $record, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV_INIT                                        !0      <array>
   70     1      > FE_RESET_R                                       $3      !0, ->10
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->10
          3    >   ASSIGN                                                   !2, ~4
   71     4        FETCH_DIM_R                                      ~6      !1, 'month1'
          5        FETCH_DIM_R                                      ~8      !0, !2
          6        ASSIGN_DIM                                               !0, ~6
          7        OP_DATA                                                  ~8
   72     8        UNSET_DIM                                                !0, !2
   70     9      > JMP                                                      ->2
         10    >   FE_FREE                                                  $3
   74    11        VERIFY_RETURN_TYPE                                       !0
         12      > RETURN                                                   !0
   75    13*       VERIFY_RETURN_TYPE                                       
         14*     > RETURN                                                   null

End of function rekey

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.03 ms | 1010 KiB | 17 Q