3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sumArray($array, $index, $col) { $returnArray = []; // temporary container // sanity checks if (!is_array($array)) { return 'error not an array'; } $firstRow = reset($array); if (!array_key_exists($index, $firstRow) || !array_key_exists($col, $firstRow)) { return 'error keys provided not found'; } foreach ($array as $value) { if (!isset($returnArray[$value[$index]])) { // initialize $returnArray[$value[$index]] = [$index => $value[$index], $col => 0]; } // add value $returnArray[$value[$index]][$col] += $value[$col]; } return $returnArray; } $products = array ( array("Id" => "000001", "Name" => "Cheese", "Quantity" => "10", "Price" => "10"), array("Id" => "000001", "Name" => "Cheese", "Quantity" => "20", "Price" => "20"), array("Id" => "000001", "Name" => "Cheese", "Quantity" => "10", "Price" => "30"), array("Id" => "000002", "Name" => "Ham", "Quantity" => "20", "Price" => "200"), array("Id" => "000002", "Name" => "Ham", "Quantity" => "9", "Price" => "100"), array("Id" => "000003", "Name" => "Baicon", "Quantity" => "40", "Price" => "900") ); $summedArray = sumArray($products, 'Id', 'Quantity'); print_r($summedArray);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mmOWf
function name:  (null)
number of ops:  11
compiled vars:  !0 = $products, !1 = $summedArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   ASSIGN                                                   !0, <array>
   56     1        INIT_FCALL                                               'sumarray'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'Id'
          4        SEND_VAL                                                 'Quantity'
          5        DO_FCALL                                      0  $3      
          6        ASSIGN                                                   !1, $3
   57     7        INIT_FCALL                                               'print_r'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Function sumarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 8
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 47) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 77) Position 1 = 21, Position 2 = 38
Branch analysis from position: 21
2 jumps found. (Code = 78) Position 1 = 22, Position 2 = 38
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 32
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 32
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
Branch analysis from position: 18
filename:       /in/mmOWf
function name:  sumArray
number of ops:  41
compiled vars:  !0 = $array, !1 = $index, !2 = $col, !3 = $returnArray, !4 = $firstRow, !5 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    4     3        ASSIGN                                                   !3, <array>
    6     4        TYPE_CHECK                                  128  ~7      !0
          5        BOOL_NOT                                         ~8      ~7
          6      > JMPZ                                                     ~8, ->8
    7     7    > > RETURN                                                   'error+not+an+array'
    9     8    >   INIT_FCALL                                               'reset'
          9        SEND_REF                                                 !0
         10        DO_ICALL                                         $9      
         11        ASSIGN                                                   !4, $9
   10    12        ARRAY_KEY_EXISTS                                 ~11     !1, !4
         13        BOOL_NOT                                         ~12     ~11
         14      > JMPNZ_EX                                         ~12     ~12, ->18
         15    >   ARRAY_KEY_EXISTS                                 ~13     !2, !4
         16        BOOL_NOT                                         ~14     ~13
         17        BOOL                                             ~12     ~14
         18    > > JMPZ                                                     ~12, ->20
   11    19    > > RETURN                                                   'error+keys+provided+not+found'
   13    20    > > FE_RESET_R                                       $15     !0, ->38
         21    > > FE_FETCH_R                                               $15, !5, ->38
   14    22    >   FETCH_DIM_R                                      ~16     !5, !1
         23        ISSET_ISEMPTY_DIM_OBJ                         0  ~17     !3, ~16
         24        BOOL_NOT                                         ~18     ~17
         25      > JMPZ                                                     ~18, ->32
   15    26    >   FETCH_DIM_R                                      ~19     !5, !1
         27        FETCH_DIM_R                                      ~21     !5, !1
         28        INIT_ARRAY                                       ~22     ~21, !1
         29        ADD_ARRAY_ELEMENT                                ~22     0, !2
         30        ASSIGN_DIM                                               !3, ~19
         31        OP_DATA                                                  ~22
   18    32    >   FETCH_DIM_R                                      ~23     !5, !1
         33        FETCH_DIM_R                                      ~26     !5, !2
         34        FETCH_DIM_RW                                     $24     !3, ~23
         35        ASSIGN_DIM_OP                +=               1          $24, !2
         36        OP_DATA                                                  ~26
   13    37      > JMP                                                      ->21
         38    >   FE_FREE                                                  $15
   21    39      > RETURN                                                   !3
   22    40*     > RETURN                                                   null

End of function sumarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
282.88 ms | 1006 KiB | 16 Q