3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array( array( "name" => "SomeName", "type" => "A"), array( "name" => "SomeName", "type" => "A"), array( "name" => "SomeName", "type" => "A"), array( "name" => "SomeName", "type" => "A"), array( "name" => "SomeName", "type" => "A"), array( "name" => "SomeName", "type" => "B"), array( "name" => "SomeName", "type" => "B"), array( "name" => "SomeName", "type" => "B"), array( "name" => "SomeName", "type" => "C"), array( "name" => "SomeName", "type" => "C") ); // split data into arrays of distinct type $buckets = array_reduce($data, function($result, $item) { $type = $item["type"]; if (!isset($result[$type])) { $result[$type] = []; } array_push($result[$type], $item); return $result; }, []); // sort buckets by size usort($buckets, function($a, $b) { return count($b) - count($a); }); // merge buckets to single array sorted by type // and split to chunks of size of the largest bucket $table = array_chunk(array_merge(...$buckets), count($buckets[0])); // compute final array by merging each column $result = []; foreach (array_keys($table[0]) as $i) { $result = array_merge($result, array_column($table, $i)); } print_r($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 41
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 41
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
filename:       /in/IJrQH
function name:  (null)
number of ops:  46
compiled vars:  !0 = $data, !1 = $buckets, !2 = $table, !3 = $result, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   17     1        INIT_FCALL                                               'array_reduce'
          2        SEND_VAR                                                 !0
          3        DECLARE_LAMBDA_FUNCTION                          ~6      [0]
   24     4        SEND_VAL                                                 ~6
          5        SEND_VAL                                                 <array>
   17     6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !1, $7
   26     8        INIT_FCALL                                               'usort'
          9        SEND_REF                                                 !1
         10        DECLARE_LAMBDA_FUNCTION                          ~9      [1]
   28    11        SEND_VAL                                                 ~9
   26    12        DO_ICALL                                                 
   31    13        INIT_FCALL                                               'array_chunk'
         14        INIT_FCALL                                               'array_merge'
         15        SEND_UNPACK                                              !1
         16        CHECK_UNDEF_ARGS                                         
         17        DO_ICALL                                         $11     
         18        SEND_VAR                                                 $11
         19        FETCH_DIM_R                                      ~12     !1, 0
         20        COUNT                                            ~13     ~12
         21        SEND_VAL                                                 ~13
         22        DO_ICALL                                         $14     
         23        ASSIGN                                                   !2, $14
   33    24        ASSIGN                                                   !3, <array>
   34    25        INIT_FCALL                                               'array_keys'
         26        FETCH_DIM_R                                      ~17     !2, 0
         27        SEND_VAL                                                 ~17
         28        DO_ICALL                                         $18     
         29      > FE_RESET_R                                       $19     $18, ->41
         30    > > FE_FETCH_R                                               $19, !4, ->41
   35    31    >   INIT_FCALL                                               'array_merge'
         32        SEND_VAR                                                 !3
         33        INIT_FCALL                                               'array_column'
         34        SEND_VAR                                                 !2
         35        SEND_VAR                                                 !4
         36        DO_ICALL                                         $20     
         37        SEND_VAR                                                 $20
         38        DO_ICALL                                         $21     
         39        ASSIGN                                                   !3, $21
   34    40      > JMP                                                      ->30
         41    >   FE_FREE                                                  $19
   37    42        INIT_FCALL                                               'print_r'
         43        SEND_VAR                                                 !3
         44        DO_ICALL                                                 
         45      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/IJrQH
function name:  {closure}
number of ops:  16
compiled vars:  !0 = $result, !1 = $item, !2 = $type
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        FETCH_DIM_R                                      ~3      !1, 'type'
          3        ASSIGN                                                   !2, ~3
   19     4        ISSET_ISEMPTY_DIM_OBJ                         0  ~5      !0, !2
          5        BOOL_NOT                                         ~6      ~5
          6      > JMPZ                                                     ~6, ->9
   20     7    >   ASSIGN_DIM                                               !0, !2
          8        OP_DATA                                                  <array>
   22     9    >   INIT_FCALL                                               'array_push'
         10        FETCH_DIM_W                                      $8      !0, !2
         11        SEND_REF                                                 $8
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                                 
   23    14      > RETURN                                                   !0
   24    15*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IJrQH
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   27     2        COUNT                                            ~2      !1
          3        COUNT                                            ~3      !0
          4        SUB                                              ~4      ~2, ~3
          5      > RETURN                                                   ~4
   28     6*     > RETURN                                                   null

End of Dynamic Function 1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.58 ms | 1466 KiB | 21 Q