3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = array ( 0 => array ( 'r_id' => 11, 'r_sid' => 'RC', 'd_id' => 2 ), 1 => array ( 'r_id' => 7, 'r_sid' => 'RC', 'c_id' => 51 ), 2 => array ( 'r_id' => 6, 'r_sid' => 'JN', 'c_id' => 52 ), 3 => array ( 'r_id' => 7, 'r_sid' => 'JN', 'c_id' => 51 ), 4 => array ( 'r_id' => 7, 'r_sid' => 'LG', 'c_id' => 51 ), 5 => array ( 'r_id' => 7, 'r_sid' => 'BN', 'c_id' => 51 ), 6 => array ( 'r_id' => 6, 'r_sid' => 'IVS', 'c_id' => 52 ), 7 => array ( 'r_id' => 7, 'r_sid' => 'IVS', 'c_id' => 51 ) ); // filter out r_sid values which only occur once $sid_values = array_count_values(array_column($array, 'r_sid')); $array = array_filter($array, function ($a) use ($sid_values) { return $sid_values[$a['r_sid']] > 1; }); // combine arrays on r_sid values $result = array(); foreach ($array as $arr) { $sid = $arr['r_sid']; // push data to arrays $result[$sid]['r_sid'] = $sid; $result[$sid]['r_id'][] = $arr['r_id']; if (isset($arr['c_id'])) $result[$sid]['c_id'][] = $arr['c_id']; if (isset($arr['d_id'])) $result[$sid]['d_id'][] = $arr['d_id']; } // combine all the c_id values into a string array_walk($result, function (&$a) { if (isset($a['c_id'])) $a['c_id'] = implode(',', $a['c_id']); }); // now combine any r_sid values that have the same set of c_id values $output = array(); foreach ($result as $res) { $cid = $res['c_id']; // push data to arrays $output[$cid]['c_id'] = $cid; $output[$cid]['r_sid'] = array_merge($output[$cid]['r_sid'] ?? array(), array($res['r_sid'])); $output[$cid]['r_id'] = array_merge($output[$cid]['r_id'] ?? array(), $res['r_id']); if (isset($res['d_id'])) $output[$cid]['d_id'] = array_merge($output[$cid]['d_id'] ?? array(), $res['d_id']); } // combine the r_sid, r_id and d_id values into a string array_walk($output, function (&$a) { $a['r_sid'] = implode(',', $a['r_sid']); $a['r_id'] = implode(',', array_unique($a['r_id'])); if (isset($a['d_id'])) $a['d_id'] = implode(',', $a['d_id']); }); // remove the associative keys $output = array_values($output); print_r($output);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 44
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 44
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 36
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 43
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 43
Branch analysis from position: 36
Branch analysis from position: 44
2 jumps found. (Code = 77) Position 1 = 52, Position 2 = 98
Branch analysis from position: 52
2 jumps found. (Code = 78) Position 1 = 53, Position 2 = 98
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 97
Branch analysis from position: 85
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
Branch analysis from position: 97
Branch analysis from position: 98
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 98
Branch analysis from position: 44
filename:       /in/1Bq8e
function name:  (null)
number of ops:  112
compiled vars:  !0 = $array, !1 = $sid_values, !2 = $result, !3 = $arr, !4 = $sid, !5 = $output, !6 = $res, !7 = $cid
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   55     1        INIT_FCALL                                               'array_count_values'
          2        INIT_FCALL                                               'array_column'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'r_sid'
          5        DO_ICALL                                         $9      
          6        SEND_VAR                                                 $9
          7        DO_ICALL                                         $10     
          8        ASSIGN                                                   !1, $10
   56     9        INIT_FCALL                                               'array_filter'
         10        SEND_VAR                                                 !0
         11        DECLARE_LAMBDA_FUNCTION                          ~12     [0]
         12        BIND_LEXICAL                                             ~12, !1
         13        SEND_VAL                                                 ~12
         14        DO_ICALL                                         $13     
         15        ASSIGN                                                   !0, $13
   59    16        ASSIGN                                                   !2, <array>
   60    17      > FE_RESET_R                                       $16     !0, ->44
         18    > > FE_FETCH_R                                               $16, !3, ->44
   61    19    >   FETCH_DIM_R                                      ~17     !3, 'r_sid'
         20        ASSIGN                                                   !4, ~17
   63    21        FETCH_DIM_W                                      $19     !2, !4
         22        ASSIGN_DIM                                               $19, 'r_sid'
         23        OP_DATA                                                  !4
   64    24        FETCH_DIM_R                                      ~24     !3, 'r_id'
         25        FETCH_DIM_W                                      $21     !2, !4
         26        FETCH_DIM_W                                      $22     $21, 'r_id'
         27        ASSIGN_DIM                                               $22
         28        OP_DATA                                                  ~24
   65    29        ISSET_ISEMPTY_DIM_OBJ                         0          !3, 'c_id'
         30      > JMPZ                                                     ~25, ->36
         31    >   FETCH_DIM_R                                      ~29     !3, 'c_id'
         32        FETCH_DIM_W                                      $26     !2, !4
         33        FETCH_DIM_W                                      $27     $26, 'c_id'
         34        ASSIGN_DIM                                               $27
         35        OP_DATA                                                  ~29
   66    36    >   ISSET_ISEMPTY_DIM_OBJ                         0          !3, 'd_id'
         37      > JMPZ                                                     ~30, ->43
         38    >   FETCH_DIM_R                                      ~34     !3, 'd_id'
         39        FETCH_DIM_W                                      $31     !2, !4
         40        FETCH_DIM_W                                      $32     $31, 'd_id'
         41        ASSIGN_DIM                                               $32
         42        OP_DATA                                                  ~34
   60    43    > > JMP                                                      ->18
         44    >   FE_FREE                                                  $16
   70    45        INIT_FCALL                                               'array_walk'
         46        SEND_REF                                                 !2
         47        DECLARE_LAMBDA_FUNCTION                          ~35     [1]
   72    48        SEND_VAL                                                 ~35
   70    49        DO_ICALL                                                 
   75    50        ASSIGN                                                   !5, <array>
   76    51      > FE_RESET_R                                       $38     !2, ->98
         52    > > FE_FETCH_R                                               $38, !6, ->98
   77    53    >   FETCH_DIM_R                                      ~39     !6, 'c_id'
         54        ASSIGN                                                   !7, ~39
   79    55        FETCH_DIM_W                                      $41     !5, !7
         56        ASSIGN_DIM                                               $41, 'c_id'
         57        OP_DATA                                                  !7
   80    58        INIT_FCALL                                               'array_merge'
         59        FETCH_DIM_IS                                     ~45     !5, !7
         60        FETCH_DIM_IS                                     ~46     ~45, 'r_sid'
         61        COALESCE                                         ~47     ~46
         62        QM_ASSIGN                                        ~47     <array>
         63        SEND_VAL                                                 ~47
         64        FETCH_DIM_R                                      ~48     !6, 'r_sid'
         65        INIT_ARRAY                                       ~49     ~48
         66        SEND_VAL                                                 ~49
         67        DO_ICALL                                         $50     
         68        FETCH_DIM_W                                      $43     !5, !7
         69        ASSIGN_DIM                                               $43, 'r_sid'
         70        OP_DATA                                                  $50
   81    71        INIT_FCALL                                               'array_merge'
         72        FETCH_DIM_IS                                     ~53     !5, !7
         73        FETCH_DIM_IS                                     ~54     ~53, 'r_id'
         74        COALESCE                                         ~55     ~54
         75        QM_ASSIGN                                        ~55     <array>
         76        SEND_VAL                                                 ~55
         77        FETCH_DIM_R                                      ~56     !6, 'r_id'
         78        SEND_VAL                                                 ~56
         79        DO_ICALL                                         $57     
         80        FETCH_DIM_W                                      $51     !5, !7
         81        ASSIGN_DIM                                               $51, 'r_id'
         82        OP_DATA                                                  $57
   82    83        ISSET_ISEMPTY_DIM_OBJ                         0          !6, 'd_id'
         84      > JMPZ                                                     ~58, ->97
         85    >   INIT_FCALL                                               'array_merge'
         86        FETCH_DIM_IS                                     ~61     !5, !7
         87        FETCH_DIM_IS                                     ~62     ~61, 'd_id'
         88        COALESCE                                         ~63     ~62
         89        QM_ASSIGN                                        ~63     <array>
         90        SEND_VAL                                                 ~63
         91        FETCH_DIM_R                                      ~64     !6, 'd_id'
         92        SEND_VAL                                                 ~64
         93        DO_ICALL                                         $65     
         94        FETCH_DIM_W                                      $59     !5, !7
         95        ASSIGN_DIM                                               $59, 'd_id'
         96        OP_DATA                                                  $65
   76    97    > > JMP                                                      ->52
         98    >   FE_FREE                                                  $38
   86    99        INIT_FCALL                                               'array_walk'
        100        SEND_REF                                                 !5
        101        DECLARE_LAMBDA_FUNCTION                          ~66     [2]
   90   102        SEND_VAL                                                 ~66
   86   103        DO_ICALL                                                 
   93   104        INIT_FCALL                                               'array_values'
        105        SEND_VAR                                                 !5
        106        DO_ICALL                                         $68     
        107        ASSIGN                                                   !5, $68
   95   108        INIT_FCALL                                               'print_r'
        109        SEND_VAR                                                 !5
        110        DO_ICALL                                                 
        111      > 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/1Bq8e
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $a, !1 = $sid_values
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_DIM_R                                      ~2      !0, 'r_sid'
          3        FETCH_DIM_R                                      ~3      !1, ~2
          4        IS_SMALLER                                       ~4      1, ~3
          5      > RETURN                                                   ~4
          6*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/1Bq8e
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
   71     1        ISSET_ISEMPTY_DIM_OBJ                         0          !0, 'c_id'
          2      > JMPZ                                                     ~1, ->10
          3    >   INIT_FCALL                                               'implode'
          4        SEND_VAL                                                 '%2C'
          5        FETCH_DIM_R                                      ~3      !0, 'c_id'
          6        SEND_VAL                                                 ~3
          7        DO_ICALL                                         $4      
          8        ASSIGN_DIM                                               !0, 'c_id'
          9        OP_DATA                                                  $4
   72    10    > > RETURN                                                   null

End of Dynamic Function 1

Dynamic Function 2
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 27
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/1Bq8e
function name:  {closure}
number of ops:  28
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
   87     1        INIT_FCALL                                               'implode'
          2        SEND_VAL                                                 '%2C'
          3        FETCH_DIM_R                                      ~2      !0, 'r_sid'
          4        SEND_VAL                                                 ~2
          5        DO_ICALL                                         $3      
          6        ASSIGN_DIM                                               !0, 'r_sid'
          7        OP_DATA                                                  $3
   88     8        INIT_FCALL                                               'implode'
          9        SEND_VAL                                                 '%2C'
         10        INIT_FCALL                                               'array_unique'
         11        FETCH_DIM_R                                      ~5      !0, 'r_id'
         12        SEND_VAL                                                 ~5
         13        DO_ICALL                                         $6      
         14        SEND_VAR                                                 $6
         15        DO_ICALL                                         $7      
         16        ASSIGN_DIM                                               !0, 'r_id'
         17        OP_DATA                                                  $7
   89    18        ISSET_ISEMPTY_DIM_OBJ                         0          !0, 'd_id'
         19      > JMPZ                                                     ~8, ->27
         20    >   INIT_FCALL                                               'implode'
         21        SEND_VAL                                                 '%2C'
         22        FETCH_DIM_R                                      ~10     !0, 'd_id'
         23        SEND_VAL                                                 ~10
         24        DO_ICALL                                         $11     
         25        ASSIGN_DIM                                               !0, 'd_id'
         26        OP_DATA                                                  $11
   90    27    > > RETURN                                                   null

End of Dynamic Function 2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.84 ms | 1022 KiB | 22 Q