3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = array( 'code 10 and amount 1000', 'code 10 and amount 200', 'code 10 and amount 300', 'code 20 and amount 2000' ); $output = array(); foreach ($array as $value) { // Extract the values from the string preg_match('/^code\s(.*)\s\sand amount\s(.*)$/', $value, $match); // If no code exists, we have a problem so skip the line if (! isset($match[1])) { continue; } // Make this a bit more readable for future devs $code = (int) $match[1]; $amount = (int) isset($match[2]) ? $match[2] : 0; // If the code exists in the output array, add the value to it if (isset($output[$code])) { $output[$code] += $amount; } else { // Else make the new code have the value $output[$code] = $amount; } } print_r($output);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 32
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 32
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 29
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 29
Branch analysis from position: 26
Branch analysis from position: 29
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/afM8Y
function name:  (null)
number of ops:  37
compiled vars:  !0 = $array, !1 = $output, !2 = $value, !3 = $match, !4 = $code, !5 = $amount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
    9     1        ASSIGN                                                   !1, <array>
   11     2      > FE_RESET_R                                       $8      !0, ->32
          3    > > FE_FETCH_R                                               $8, !2, ->32
   13     4    >   INIT_FCALL                                               'preg_match'
          5        SEND_VAL                                                 '%2F%5Ecode%5Cs%28.%2A%29%5Cs%5Csand+amount%5Cs%28.%2A%29%24%2F'
          6        SEND_VAR                                                 !2
          7        SEND_REF                                                 !3
          8        DO_ICALL                                                 
   15     9        ISSET_ISEMPTY_DIM_OBJ                         0  ~10     !3, 1
         10        BOOL_NOT                                         ~11     ~10
         11      > JMPZ                                                     ~11, ->13
   16    12    > > JMP                                                      ->3
   19    13    >   FETCH_DIM_R                                      ~12     !3, 1
         14        CAST                                          4  ~13     ~12
         15        ASSIGN                                                   !4, ~13
   20    16        ISSET_ISEMPTY_DIM_OBJ                         0  ~15     !3, 2
         17        CAST                                          4  ~16     ~15
         18      > JMPZ                                                     ~16, ->22
         19    >   FETCH_DIM_R                                      ~17     !3, 2
         20        QM_ASSIGN                                        ~18     ~17
         21      > JMP                                                      ->23
         22    >   QM_ASSIGN                                        ~18     0
         23    >   ASSIGN                                                   !5, ~18
   22    24        ISSET_ISEMPTY_DIM_OBJ                         0          !1, !4
         25      > JMPZ                                                     ~20, ->29
   23    26    >   ASSIGN_DIM_OP                +=               1          !1, !4
         27        OP_DATA                                                  !5
         28      > JMP                                                      ->31
   26    29    >   ASSIGN_DIM                                               !1, !4
         30        OP_DATA                                                  !5
   11    31    > > JMP                                                      ->3
         32    >   FE_FREE                                                  $8
   30    33        INIT_FCALL                                               'print_r'
         34        SEND_VAR                                                 !1
         35        DO_ICALL                                                 
         36      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.66 ms | 1400 KiB | 17 Q