3v4l.org

run code in 300+ PHP versions simultaneously
<?php $sampleArray = [ ["id" => 0.1, "marks" => 2], ["id" => 0.2, "marks" => 4], ["id" => 0.3, "marks" => 1], ["id" => 1.1, "marks" => 6], ["id" => 1.2, "marks" => 0], ["id" => 1.8, "marks" => 8], ["id" => 2.1, "marks" => 7], ["id" => 2.2, "marks" => 12], ]; $result = []; foreach ($sampleArray as $row) { if ($result && !isset($result[$row['id']])) { // $result is not empty and current id is not yet in output array $lastKey = array_key_last($result); echo "Sum of ID $lastKey is {$result[$lastKey]}<br><br>"; } echo "id: {$row['id']}, marks: {$row['marks']}<br>"; $result[$row['id']] = ($result[$row['id']] ?? 0) + $row['marks']; } if ($result) { $lastKey = array_key_last($result); echo "Sum of ID $lastKey is {$result[$lastKey]}"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 39
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 39
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 21
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 21
Branch analysis from position: 9
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 51
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
Branch analysis from position: 39
filename:       /in/aT4eU
function name:  (null)
number of ops:  52
compiled vars:  !0 = $sampleArray, !1 = $result, !2 = $row, !3 = $lastKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   14     1        ASSIGN                                                   !1, <array>
   15     2      > FE_RESET_R                                       $6      !0, ->39
          3    > > FE_FETCH_R                                               $6, !2, ->39
   16     4    > > JMPZ_EX                                          ~7      !1, ->9
          5    >   FETCH_DIM_R                                      ~8      !2, 'id'
          6        ISSET_ISEMPTY_DIM_OBJ                         0  ~9      !1, ~8
          7        BOOL_NOT                                         ~10     ~9
          8        BOOL                                             ~7      ~10
          9    > > JMPZ                                                     ~7, ->21
   17    10    >   INIT_FCALL                                               'array_key_last'
         11        SEND_VAR                                                 !1
         12        DO_ICALL                                         $11     
         13        ASSIGN                                                   !3, $11
   18    14        ROPE_INIT                                     5  ~15     'Sum+of+ID+'
         15        ROPE_ADD                                      1  ~15     ~15, !3
         16        ROPE_ADD                                      2  ~15     ~15, '+is+'
         17        FETCH_DIM_R                                      ~13     !1, !3
         18        ROPE_ADD                                      3  ~15     ~15, ~13
         19        ROPE_END                                      4  ~14     ~15, '%3Cbr%3E%3Cbr%3E'
         20        ECHO                                                     ~14
   20    21    >   ROPE_INIT                                     5  ~21     'id%3A+'
         22        FETCH_DIM_R                                      ~18     !2, 'id'
         23        ROPE_ADD                                      1  ~21     ~21, ~18
         24        ROPE_ADD                                      2  ~21     ~21, '%2C+marks%3A+'
         25        FETCH_DIM_R                                      ~19     !2, 'marks'
         26        ROPE_ADD                                      3  ~21     ~21, ~19
         27        ROPE_END                                      4  ~20     ~21, '%3Cbr%3E'
         28        ECHO                                                     ~20
   21    29        FETCH_DIM_R                                      ~24     !2, 'id'
         30        FETCH_DIM_R                                      ~26     !2, 'id'
         31        FETCH_DIM_IS                                     ~27     !1, ~26
         32        COALESCE                                         ~28     ~27
         33        QM_ASSIGN                                        ~28     0
         34        FETCH_DIM_R                                      ~29     !2, 'marks'
         35        ADD                                              ~30     ~28, ~29
         36        ASSIGN_DIM                                               !1, ~24
         37        OP_DATA                                                  ~30
   15    38      > JMP                                                      ->3
         39    >   FE_FREE                                                  $6
   23    40      > JMPZ                                                     !1, ->51
   24    41    >   INIT_FCALL                                               'array_key_last'
         42        SEND_VAR                                                 !1
         43        DO_ICALL                                         $31     
         44        ASSIGN                                                   !3, $31
   25    45        ROPE_INIT                                     4  ~35     'Sum+of+ID+'
         46        ROPE_ADD                                      1  ~35     ~35, !3
         47        ROPE_ADD                                      2  ~35     ~35, '+is+'
         48        FETCH_DIM_R                                      ~33     !1, !3
         49        ROPE_END                                      3  ~34     ~35, ~33
         50        ECHO                                                     ~34
   26    51    > > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.21 ms | 1074 KiB | 14 Q