3v4l.org

run code in 300+ PHP versions simultaneously
<?php $stops = Array ( 0 => Array ( 'agency_stop_id' => 1, 'routes' => Array ( '_ids' => Array (0 => 'Route 1', 1 => 'Route 2' ))), 1 => Array ( 'agency_stop_id' => 5, 'routes' => Array ( '_ids' => Array (0 => 'Route 1', 1 => 'Route 2' ))), 2 => Array ( 'agency_stop_id' => 5, 'routes' => Array ( '_ids' => Array (0 => 'Route 3', 1 => 'Route 4' ))), ); $out = []; $stops = array_unique($stops, SORT_REGULAR); $stopids = array_unique(array_column($stops, 'agency_stop_id')); foreach ($stopids as $stopid) { $keys = array_keys(array_column($stops, 'agency_stop_id'), $stopid); //Create an array of the matching stops foreach ($keys as $key) { $stop = array_map( function ($k) use ($stops) { return $stops[$k]; }, $key ); } //print_r($stop); //if there is only one if (count($stop) == 1) { //save array to $out $out[] = $stop[0]; //if there is more then one } elseif (count($keys) > 1) { $stop = array_merge_recursive($stop[0], [ 'routes' => $stop[1]['routes']]); $out[] = $stop; } } print_r($out); #print_r($new_array[0]); #print_r(Array( 'routes' => $new_array[1]['routes']));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 59
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 59
Branch analysis from position: 17
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 36
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 36
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 44
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 58
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 58
Branch analysis from position: 36
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 59
filename:       /in/iMRhr
function name:  (null)
number of ops:  64
compiled vars:  !0 = $stops, !1 = $out, !2 = $stopids, !3 = $stopid, !4 = $keys, !5 = $key, !6 = $stop
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   10     1        ASSIGN                                                   !1, <array>
   11     2        INIT_FCALL                                               'array_unique'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 0
          5        DO_ICALL                                         $9      
          6        ASSIGN                                                   !0, $9
   12     7        INIT_FCALL                                               'array_unique'
          8        INIT_FCALL                                               'array_column'
          9        SEND_VAR                                                 !0
         10        SEND_VAL                                                 'agency_stop_id'
         11        DO_ICALL                                         $11     
         12        SEND_VAR                                                 $11
         13        DO_ICALL                                         $12     
         14        ASSIGN                                                   !2, $12
   13    15      > FE_RESET_R                                       $14     !2, ->59
         16    > > FE_FETCH_R                                               $14, !3, ->59
   14    17    >   INIT_FCALL                                               'array_keys'
         18        INIT_FCALL                                               'array_column'
         19        SEND_VAR                                                 !0
         20        SEND_VAL                                                 'agency_stop_id'
         21        DO_ICALL                                         $15     
         22        SEND_VAR                                                 $15
         23        SEND_VAR                                                 !3
         24        DO_ICALL                                         $16     
         25        ASSIGN                                                   !4, $16
   16    26      > FE_RESET_R                                       $18     !4, ->36
         27    > > FE_FETCH_R                                               $18, !5, ->36
   17    28    >   INIT_FCALL                                               'array_map'
   18    29        DECLARE_LAMBDA_FUNCTION                          ~19     [0]
         30        BIND_LEXICAL                                             ~19, !0
   20    31        SEND_VAL                                                 ~19
   21    32        SEND_VAR                                                 !5
   17    33        DO_ICALL                                         $20     
         34        ASSIGN                                                   !6, $20
   16    35      > JMP                                                      ->27
         36    >   FE_FREE                                                  $18
   26    37        COUNT                                            ~22     !6
         38        IS_EQUAL                                                 ~22, 1
         39      > JMPZ                                                     ~23, ->44
   28    40    >   FETCH_DIM_R                                      ~25     !6, 0
         41        ASSIGN_DIM                                               !1
         42        OP_DATA                                                  ~25
   26    43      > JMP                                                      ->58
   30    44    >   COUNT                                            ~26     !4
         45        IS_SMALLER                                               1, ~26
         46      > JMPZ                                                     ~27, ->58
   31    47    >   INIT_FCALL                                               'array_merge_recursive'
         48        FETCH_DIM_R                                      ~28     !6, 0
         49        SEND_VAL                                                 ~28
         50        FETCH_DIM_R                                      ~29     !6, 1
         51        FETCH_DIM_R                                      ~30     ~29, 'routes'
         52        INIT_ARRAY                                       ~31     ~30, 'routes'
         53        SEND_VAL                                                 ~31
         54        DO_ICALL                                         $32     
         55        ASSIGN                                                   !6, $32
   32    56        ASSIGN_DIM                                               !1
         57        OP_DATA                                                  !6
   13    58    > > JMP                                                      ->16
         59    >   FE_FREE                                                  $14
   36    60        INIT_FCALL                                               'print_r'
         61        SEND_VAR                                                 !1
         62        DO_ICALL                                                 
   38    63      > 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/iMRhr
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $k, !1 = $stops
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   19     2        FETCH_DIM_R                                      ~2      !1, !0
          3      > RETURN                                                   ~2
   20     4*     > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.06 ms | 1021 KiB | 19 Q