3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr = [ [ 'user_id' => 120, 'username' => 'test1', 'count' => 2 ], [ 'user_id' => 120, 'username' => 'test1', 'count' => 3 ], [ 'user_id' => 110, 'username' => 'test2', 'count' => 2 ] ]; /** loop through "$arr" and sum the "count" values based on the "user_id" */ $result = array_reduce($arr, function ($a, $c) { /** index the resulting array by the "user_id" value which allows for instant lookup in case of duplicate "user_id" value */ $a[$c['user_id']] = [ 'user_id' => $c['user_id'], 'username' => $c['username'], /** sum the "count" values * the first iteration will have an empty array * so we should check for existance before getting the value in order to prevent bugs */ 'count' => ($a[$c['user_id']]['count'] ?? 0) + $c['count'] ]; return $a; }, []); var_export($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IOZ6iL
function name:  (null)
number of ops:  12
compiled vars:  !0 = $arr, !1 = $result
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   22     1        INIT_FCALL                                                   'array_reduce'
          2        SEND_VAR                                                     !0
          3        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
   34     4        SEND_VAL                                                     ~3
          5        SEND_VAL                                                     <array>
   22     6        DO_ICALL                                             $4      
          7        ASSIGN                                                       !1, $4
   36     8        INIT_FCALL                                                   'var_export'
          9        SEND_VAR                                                     !1
         10        DO_ICALL                                                     
         11      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IOZ6iL
function name:  {closure:/in/IOZ6iL:22}
number of ops:  19
compiled vars:  !0 = $a, !1 = $c
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   24     2        FETCH_DIM_R                                          ~2      !1, 'user_id'
   25     3        FETCH_DIM_R                                          ~4      !1, 'user_id'
          4        INIT_ARRAY                                           ~5      ~4, 'user_id'
   26     5        FETCH_DIM_R                                          ~6      !1, 'username'
          6        ADD_ARRAY_ELEMENT                                    ~5      ~6, 'username'
   31     7        FETCH_DIM_R                                          ~7      !1, 'user_id'
          8        FETCH_DIM_IS                                         ~8      !0, ~7
          9        FETCH_DIM_IS                                         ~9      ~8, 'count'
         10        COALESCE                                             ~10     ~9
         11        QM_ASSIGN                                            ~10     0
         12        FETCH_DIM_R                                          ~11     !1, 'count'
         13        ADD                                                  ~12     ~10, ~11
         14        ADD_ARRAY_ELEMENT                                    ~5      ~12, 'count'
   24    15        ASSIGN_DIM                                                   !0, ~2
   31    16        OP_DATA                                                      ~5
   33    17      > RETURN                                                       !0
   34    18*     > RETURN                                                       null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
155.35 ms | 1498 KiB | 15 Q