3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = Array ( 0 => Array ( "year" => "2016", "numb" => 1 ), 1 => Array ( "year" => "2016", "numb" => 3 ), 2 => Array ( "year" => "2017", "numb" => 3 ), 3 => Array ( "year" => "2016", "numb" => 1 ), 4 => Array ( "year" => "2018", "numb" => 2 ) ); // create new array with year as key and numb as value $res =array(); Foreach($arr as $val){ If(!isset($res[$val["year"]])) $res[$val["year"]] =0; $res[$val["year"]] += $val["numb"]; } ksort($res); // sort the array on keys to make earliest year first $newarr= array(); $sum =0; foreach($res as $key => $val){ $newarr[$key] = $val+$sum; // new array with summed values from previous year(s) $sum +=$val; } Var_dump($newarr);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 16
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
Branch analysis from position: 16
2 jumps found. (Code = 77) Position 1 = 23, Position 2 = 30
Branch analysis from position: 23
2 jumps found. (Code = 78) Position 1 = 24, Position 2 = 30
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
Branch analysis from position: 16
filename:       /in/5Ql2U
function name:  (null)
number of ops:  35
compiled vars:  !0 = $arr, !1 = $res, !2 = $val, !3 = $newarr, !4 = $sum, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   31     1        ASSIGN                                                   !1, <array>
   32     2      > FE_RESET_R                                       $8      !0, ->16
          3    > > FE_FETCH_R                                               $8, !2, ->16
   33     4    >   FETCH_DIM_R                                      ~9      !2, 'year'
          5        ISSET_ISEMPTY_DIM_OBJ                         0  ~10     !1, ~9
          6        BOOL_NOT                                         ~11     ~10
          7      > JMPZ                                                     ~11, ->11
          8    >   FETCH_DIM_R                                      ~12     !2, 'year'
          9        ASSIGN_DIM                                               !1, ~12
         10        OP_DATA                                                  0
   34    11    >   FETCH_DIM_R                                      ~14     !2, 'year'
         12        FETCH_DIM_R                                      ~16     !2, 'numb'
         13        ASSIGN_DIM_OP                +=               1          !1, ~14
         14        OP_DATA                                                  ~16
   32    15      > JMP                                                      ->3
         16    >   FE_FREE                                                  $8
   38    17        INIT_FCALL                                               'ksort'
         18        SEND_REF                                                 !1
         19        DO_ICALL                                                 
   39    20        ASSIGN                                                   !3, <array>
   40    21        ASSIGN                                                   !4, 0
   41    22      > FE_RESET_R                                       $20     !1, ->30
         23    > > FE_FETCH_R                                       ~21     $20, !2, ->30
         24    >   ASSIGN                                                   !5, ~21
   42    25        ADD                                              ~24     !2, !4
         26        ASSIGN_DIM                                               !3, !5
         27        OP_DATA                                                  ~24
   43    28        ASSIGN_OP                                     1          !4, !2
   41    29      > JMP                                                      ->23
         30    >   FE_FREE                                                  $20
   45    31        INIT_FCALL                                               'var_dump'
         32        SEND_VAR                                                 !3
         33        DO_ICALL                                                 
         34      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.61 ms | 1006 KiB | 15 Q