3v4l.org

run code in 300+ PHP versions simultaneously
<?php function merge(array ...$sets) { /** * group contributions by name */ $contributions = array_reduce( $sets, function (array $contributions, array $set) { foreach ($set as $element) { $name = $element['P_Name']; $contribution = $element['Contribution']; if (!array_key_exists($name, $contributions)) { $contributions[$name] = []; } $contributions[$name][] = $contribution; } return $contributions; }, [] ); /** * normalize the array so we remove the name as key, and return a tuple of name and contribution, with the desired * structure */ return array_values(array_map(function (array $contribution, $name) { return [ 'Contribution' => array_sum($contribution), 'P_Name' => $name, ]; }, $contributions, array_keys($contributions))); } $a = [ [ 'Contribution' => 1000, 'P_Name' => 'A', ], [ 'Contribution' => 1500, 'P_Name' => 'B', ], ]; $b = [ [ 'Contribution' => 100, 'P_Name' => 'A', ], [ 'Contribution' => 200, 'P_Name' => 'B', ], ]; $merged = merge($a, $b); var_dump($merged);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EI4Tn
function name:  (null)
number of ops:  11
compiled vars:  !0 = $a, !1 = $b, !2 = $merged
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   ASSIGN                                                   !0, <array>
   51     1        ASSIGN                                                   !1, <array>
   62     2        INIT_FCALL                                               'merge'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !2, $5
   64     7        INIT_FCALL                                               'var_dump'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Function merge:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EI4Tn
function name:  merge
number of ops:  22
compiled vars:  !0 = $sets, !1 = $contributions
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV_VARIADIC                                    !0      
    8     1        INIT_FCALL                                               'array_reduce'
    9     2        SEND_VAR                                                 !0
   10     3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FEI4Tn%3A10%240'
   24     4        SEND_VAL                                                 ~2
   25     5        SEND_VAL                                                 <array>
          6        DO_ICALL                                         $3      
    8     7        ASSIGN                                                   !1, $3
   32     8        INIT_FCALL                                               'array_values'
          9        INIT_FCALL                                               'array_map'
         10        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FEI4Tn%3A32%241'
   37    11        SEND_VAL                                                 ~5
         12        SEND_VAR                                                 !1
         13        INIT_FCALL                                               'array_keys'
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                         $6      
         16        SEND_VAR                                                 $6
         17        DO_ICALL                                         $7      
         18        SEND_VAR                                                 $7
         19        DO_ICALL                                         $8      
         20      > RETURN                                                   $8
   38    21*     > RETURN                                                   null

End of function merge

Function %00%7Bclosure%7D%2Fin%2FEI4Tn%3A10%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 13
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/EI4Tn
function name:  {closure}
number of ops:  20
compiled vars:  !0 = $contributions, !1 = $set, !2 = $element, !3 = $name, !4 = $contribution
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2      > FE_RESET_R                                       $5      !1, ->17
          3    > > FE_FETCH_R                                               $5, !2, ->17
   12     4    >   FETCH_DIM_R                                      ~6      !2, 'P_Name'
          5        ASSIGN                                                   !3, ~6
   13     6        FETCH_DIM_R                                      ~8      !2, 'Contribution'
          7        ASSIGN                                                   !4, ~8
   15     8        ARRAY_KEY_EXISTS                                 ~10     !3, !0
          9        BOOL_NOT                                         ~11     ~10
         10      > JMPZ                                                     ~11, ->13
   16    11    >   ASSIGN_DIM                                               !0, !3
         12        OP_DATA                                                  <array>
   19    13    >   FETCH_DIM_W                                      $13     !0, !3
         14        ASSIGN_DIM                                               $13
         15        OP_DATA                                                  !4
   11    16      > JMP                                                      ->3
         17    >   FE_FREE                                                  $5
   23    18      > RETURN                                                   !0
   24    19*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FEI4Tn%3A10%240

Function %00%7Bclosure%7D%2Fin%2FEI4Tn%3A32%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EI4Tn
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $contribution, !1 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   34     2        INIT_FCALL                                               'array_sum'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $2      
          5        INIT_ARRAY                                       ~3      $2, 'Contribution'
   35     6        ADD_ARRAY_ELEMENT                                ~3      !1, 'P_Name'
          7      > RETURN                                                   ~3
   37     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FEI4Tn%3A32%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
174.14 ms | 1403 KiB | 26 Q