3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array( array( 'id' => 1, 'timestamp' => 1503050400, 'name' => 'Event A', 'value' => null, ), array( 'id' => 2, 'timestamp' => 1503446400, 'name' => 'Event B', 'value' => null, ), array( 'id' => 2, 'timestamp' => 1503446400, 'name' => 'Event B', 'value' => 71, ), array( 'id' => 3, 'timestamp' => 1503720000, 'name' => 'Event C', 'value' => 12, ), array( 'id' => 3, 'timestamp' => 1503720000, 'name' => 'Event C', 'value' => null, ), ); $buckets = array(); foreach ($data as $item) { $bucketName = serialize(array( 'id' => $item['id'], 'timestamp' => $item['timestamp'], 'name' => $item['name'], )); if (!array_key_exists($bucketName, $buckets)) { $buckets[$bucketName] = array(); } $buckets[$bucketName][] = $item; } $filtered = array_reduce( $buckets, function (array $carry, array $items) { if (1 === count($items)) { $carry[] = array_shift($items); return $carry; } $withoutNullValue = array_filter($items, function (array $item) { return array_key_exists('value', $item) && null !== $item['value']; }); if (0 < count($withoutNullValue)) { $carry = array_merge( $carry, $withoutNullValue ); return $carry; } $carry[] = array_shift($items); return $carry; }, array() ); var_dump($filtered);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 23
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 19
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/Toq75
function name:  (null)
number of ops:  35
compiled vars:  !0 = $data, !1 = $buckets, !2 = $item, !3 = $bucketName, !4 = $filtered
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   36     1        ASSIGN                                                   !1, <array>
   38     2      > FE_RESET_R                                       $7      !0, ->23
          3    > > FE_FETCH_R                                               $7, !2, ->23
   39     4    >   INIT_FCALL                                               'serialize'
   40     5        FETCH_DIM_R                                      ~8      !2, 'id'
          6        INIT_ARRAY                                       ~9      ~8, 'id'
   41     7        FETCH_DIM_R                                      ~10     !2, 'timestamp'
          8        ADD_ARRAY_ELEMENT                                ~9      ~10, 'timestamp'
   42     9        FETCH_DIM_R                                      ~11     !2, 'name'
         10        ADD_ARRAY_ELEMENT                                ~9      ~11, 'name'
         11        SEND_VAL                                                 ~9
   39    12        DO_ICALL                                         $12     
         13        ASSIGN                                                   !3, $12
   45    14        ARRAY_KEY_EXISTS                                 ~14     !3, !1
         15        BOOL_NOT                                         ~15     ~14
         16      > JMPZ                                                     ~15, ->19
   46    17    >   ASSIGN_DIM                                               !1, !3
         18        OP_DATA                                                  <array>
   49    19    >   FETCH_DIM_W                                      $17     !1, !3
         20        ASSIGN_DIM                                               $17
         21        OP_DATA                                                  !2
   38    22      > JMP                                                      ->3
         23    >   FE_FREE                                                  $7
   52    24        INIT_FCALL                                               'array_reduce'
   53    25        SEND_VAR                                                 !1
   54    26        DECLARE_LAMBDA_FUNCTION                          ~19     [0]
   77    27        SEND_VAL                                                 ~19
   78    28        SEND_VAL                                                 <array>
   52    29        DO_ICALL                                         $20     
         30        ASSIGN                                                   !4, $20
   81    31        INIT_FCALL                                               'var_dump'
         32        SEND_VAR                                                 !4
         33        DO_ICALL                                                 
         34      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 26
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Toq75
function name:  {closure}
number of ops:  33
compiled vars:  !0 = $carry, !1 = $items, !2 = $withoutNullValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   55     2        COUNT                                            ~3      !1
          3        IS_IDENTICAL                                             ~3, 1
          4      > JMPZ                                                     ~4, ->11
   56     5    >   INIT_FCALL                                               'array_shift'
          6        SEND_REF                                                 !1
          7        DO_ICALL                                         $6      
          8        ASSIGN_DIM                                               !0
          9        OP_DATA                                                  $6
   58    10      > RETURN                                                   !0
   61    11    >   INIT_FCALL                                               'array_filter'
         12        SEND_VAR                                                 !1
         13        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
   63    14        SEND_VAL                                                 ~7
   61    15        DO_ICALL                                         $8      
         16        ASSIGN                                                   !2, $8
   65    17        COUNT                                            ~10     !2
         18        IS_SMALLER                                               0, ~10
         19      > JMPZ                                                     ~11, ->26
   66    20    >   INIT_FCALL                                               'array_merge'
   67    21        SEND_VAR                                                 !0
   68    22        SEND_VAR                                                 !2
   66    23        DO_ICALL                                         $12     
         24        ASSIGN                                                   !0, $12
   71    25      > RETURN                                                   !0
   74    26    >   INIT_FCALL                                               'array_shift'
         27        SEND_REF                                                 !1
         28        DO_ICALL                                         $15     
         29        ASSIGN_DIM                                               !0
         30        OP_DATA                                                  $15
   76    31      > RETURN                                                   !0
   77    32*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/Toq75
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   62     1        ARRAY_KEY_EXISTS                                 ~1      'value', !0
          2      > JMPZ_EX                                          ~1      ~1, ->6
          3    >   FETCH_DIM_R                                      ~2      !0, 'value'
          4        TYPE_CHECK                                  1020  ~3      ~2
          5        BOOL                                             ~1      ~3
          6    > > RETURN                                                   ~1
   63     7*     > RETURN                                                   null

End of Dynamic Function 0

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.06 ms | 1012 KiB | 19 Q