3v4l.org

run code in 300+ PHP versions simultaneously
<?php $groupByCols = ['a', 'b']; $rows = [ [ 'a' => 1, 'b' => 10, 'c' => 100 ], [ 'a' => 1, 'b' => 20, 'c' => 200 ], [ 'a' => 1, 'b' => 20, 'c' => 300 ], [ 'a' => 1, 'b' => 30, 'c' => 400 ], [ 'a' => 2, 'b' => 40, 'c' => 500 ], [ 'a' => 2, 'b' => 50, 'c' => 600 ], [ 'a' => 3, 'b' => 60, 'c' => 700 ] ]; function group_by($array, $columns) { $new_arr = array(); $column = array_shift($columns); foreach ($array as $arr) { $new_arr[$arr[$column]][] = $arr; } if (count($columns)) { foreach ($new_arr as &$arr) { $arr = group_by($arr, $columns); } } return array_values($new_arr); } print_r(group_by($rows, $groupByCols));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JP2ee
function name:  (null)
number of ops:  10
compiled vars:  !0 = $groupByCols, !1 = $rows
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    4     1        ASSIGN                                                   !1, <array>
   54     2        INIT_FCALL                                               'print_r'
          3        INIT_FCALL                                               'group_by'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
          9      > RETURN                                                   1

Function group_by:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 14
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 26
Branch analysis from position: 17
2 jumps found. (Code = 125) Position 1 = 18, Position 2 = 25
Branch analysis from position: 18
2 jumps found. (Code = 126) Position 1 = 19, Position 2 = 25
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Branch analysis from position: 26
Branch analysis from position: 14
filename:       /in/JP2ee
function name:  group_by
number of ops:  31
compiled vars:  !0 = $array, !1 = $columns, !2 = $new_arr, !3 = $column, !4 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   42     2        ASSIGN                                                   !2, <array>
   43     3        INIT_FCALL                                               'array_shift'
          4        SEND_REF                                                 !1
          5        DO_ICALL                                         $6      
          6        ASSIGN                                                   !3, $6
   44     7      > FE_RESET_R                                       $8      !0, ->14
          8    > > FE_FETCH_R                                               $8, !4, ->14
   45     9    >   FETCH_DIM_R                                      ~9      !4, !3
         10        FETCH_DIM_W                                      $10     !2, ~9
         11        ASSIGN_DIM                                               $10
         12        OP_DATA                                                  !4
   44    13      > JMP                                                      ->8
         14    >   FE_FREE                                                  $8
   47    15        COUNT                                            ~12     !1
         16      > JMPZ                                                     ~12, ->26
   48    17    > > FE_RESET_RW                                      $13     !2, ->25
         18    > > FE_FETCH_RW                                              $13, !4, ->25
   49    19    >   INIT_FCALL_BY_NAME                                       'group_by'
         20        SEND_VAR_EX                                              !4
         21        SEND_VAR_EX                                              !1
         22        DO_FCALL                                      0  $14     
         23        ASSIGN                                                   !4, $14
   48    24      > JMP                                                      ->18
         25    >   FE_FREE                                                  $13
   52    26    >   INIT_FCALL                                               'array_values'
         27        SEND_VAR                                                 !2
         28        DO_ICALL                                         $16     
         29      > RETURN                                                   $16
   53    30*     > RETURN                                                   null

End of function group_by

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.49 ms | 1403 KiB | 20 Q