3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ [ "id" => 1, "influencer_user_id" => 4, "content_data" => '{"Reach":"300","Views":"320","Views through rate":"350","Shares":"350"}', ], [ "id" => 2, "influencer_user_id" => 4, "content_data" => '{"Reach":"100","Likes":"100","Views":"100"}', ], [ "id" => 3, "influencer_user_id" => 5, "content_data" => '{"Reach":"350"}', ], ]; $result = array(); // first, group the influencer_user_id foreach ($data as $element) { $result[$element['influencer_user_id']][] = $element; } // second, calculate content_data $result = array_map(function($element){ $element = array_reduce($element, function($carry, $item){ // convert to array $content_data = json_decode($item['content_data'], true); // convert content_data to integer values $content_data = array_map('intval', $content_data); $item['content_data'] = $content_data; if(empty($carry)) return $item; // compute content_data sum array_walk($item['content_data'], function(&$value, $key) use ($carry){ if(isset($carry['content_data'][$key])){ $value += $carry['content_data'][$key]; } }); return $item; }); // convert content_data back to json $cdata = $element['content_data']; $element['content_data'] = json_encode($cdata); return $element; }, $result); $result = array_values($result); print_r($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/1aETf
function name:  (null)
number of ops:  24
compiled vars:  !0 = $data, !1 = $result, !2 = $element
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   21     1        ASSIGN                                                   !1, <array>
   24     2      > FE_RESET_R                                       $5      !0, ->9
          3    > > FE_FETCH_R                                               $5, !2, ->9
   25     4    >   FETCH_DIM_R                                      ~6      !2, 'influencer_user_id'
          5        FETCH_DIM_W                                      $7      !1, ~6
          6        ASSIGN_DIM                                               $7
          7        OP_DATA                                                  !2
   24     8      > JMP                                                      ->3
          9    >   FE_FREE                                                  $5
   29    10        INIT_FCALL                                               'array_map'
         11        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1aETf%3A29%240'
   58    12        SEND_VAL                                                 ~9
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $10     
   29    15        ASSIGN                                                   !1, $10
   60    16        INIT_FCALL                                               'array_values'
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                         $12     
         19        ASSIGN                                                   !1, $12
   62    20        INIT_FCALL                                               'print_r'
         21        SEND_VAR                                                 !1
         22        DO_ICALL                                                 
         23      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F1aETf%3A29%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1aETf
function name:  {closure}
number of ops:  16
compiled vars:  !0 = $element, !1 = $cdata
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   30     1        INIT_FCALL                                               'array_reduce'
          2        SEND_VAR                                                 !0
          3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1aETf%3A30%241'
   51     4        SEND_VAL                                                 ~2
          5        DO_ICALL                                         $3      
   30     6        ASSIGN                                                   !0, $3
   54     7        FETCH_DIM_R                                      ~5      !0, 'content_data'
          8        ASSIGN                                                   !1, ~5
   55     9        INIT_FCALL                                               'json_encode'
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $8      
         12        ASSIGN_DIM                                               !0, 'content_data'
         13        OP_DATA                                                  $8
   57    14      > RETURN                                                   !0
   58    15*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1aETf%3A29%240

Function %00%7Bclosure%7D%2Fin%2F1aETf%3A30%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1aETf
function name:  {closure}
number of ops:  27
compiled vars:  !0 = $carry, !1 = $item, !2 = $content_data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   33     2        INIT_FCALL                                               'json_decode'
          3        FETCH_DIM_R                                      ~3      !1, 'content_data'
          4        SEND_VAL                                                 ~3
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $4      
          7        ASSIGN                                                   !2, $4
   36     8        INIT_FCALL                                               'array_map'
          9        SEND_VAL                                                 'intval'
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                         $6      
         12        ASSIGN                                                   !2, $6
   38    13        ASSIGN_DIM                                               !1, 'content_data'
         14        OP_DATA                                                  !2
   40    15        ISSET_ISEMPTY_CV                                         !0
         16      > JMPZ                                                     ~9, ->18
   41    17    > > RETURN                                                   !1
   44    18    >   INIT_FCALL                                               'array_walk'
         19        FETCH_DIM_W                                      $10     !1, 'content_data'
         20        SEND_REF                                                 $10
         21        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1aETf%3A44%242'
         22        BIND_LEXICAL                                             ~11, !0
   48    23        SEND_VAL                                                 ~11
         24        DO_ICALL                                                 
   50    25      > RETURN                                                   !1
   51    26*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1aETf%3A30%241

Function %00%7Bclosure%7D%2Fin%2F1aETf%3A44%242:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/1aETf
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $value, !1 = $key, !2 = $carry
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   45     3        FETCH_DIM_IS                                     ~3      !2, 'content_data'
          4        ISSET_ISEMPTY_DIM_OBJ                         0          ~3, !1
          5      > JMPZ                                                     ~4, ->9
   46     6    >   FETCH_DIM_R                                      ~5      !2, 'content_data'
          7        FETCH_DIM_R                                      ~6      ~5, !1
          8        ASSIGN_OP                                     1          !0, ~6
   48     9    > > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1aETf%3A44%242

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
186.3 ms | 1409 KiB | 27 Q