3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array( array( "name" => "SomeName1", "type" => "A"), array( "name" => "SomeName2", "type" => "A"), array( "name" => "SomeName3", "type" => "A"), array( "name" => "SomeName4", "type" => "A"), array( "name" => "SomeName5", "type" => "A"), array( "name" => "SomeName6", "type" => "B"), array( "name" => "SomeName7", "type" => "B"), array( "name" => "SomeName8", "type" => "B"), array( "name" => "SomeName9", "type" => "C"), array( "name" => "SomeName0", "type" => "C") ); $dataSorted = array(); $counts = array(); foreach($data as $elem) { // just init values for a new type if(!isset($counts[$elem['type']])) { $counts[$elem['type']] = 0; $dataByType[$elem['type']] = array(); } // count types $counts[$elem['type']]++; // save it to grouped array $dataByType[$elem['type']][] = $elem; } // sort it to A=>5, B=>3 C=>2 arsort($counts, SORT_NUMERIC); // get sorted types as an array $types = array_keys($counts); // index will be looped 0 -> count($types) - 1 and then down to 0 again $currentTypeIndex = 0; // make a walk on sorted array. First get the most popular, then less popular etc. // when all types are added, repeat while(count($dataSorted) < count($data)) { $currentType = $types[$currentTypeIndex]; // skip adding if we ran out this type if($counts[$currentType]) { // pop an element of selected type $dataSorted[] = array_pop($dataByType[$currentType]); // decrease counter $counts[$currentType]--; } // choose next type $currentTypeIndex = (++$currentTypeIndex)%count($types); } print_r($dataSorted);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 23
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 15
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 34
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 46
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 34
Branch analysis from position: 54
Branch analysis from position: 34
Branch analysis from position: 46
Branch analysis from position: 23
filename:       /in/uS5ru
function name:  (null)
number of ops:  58
compiled vars:  !0 = $data, !1 = $dataSorted, !2 = $counts, !3 = $elem, !4 = $dataByType, !5 = $types, !6 = $currentTypeIndex, !7 = $currentType
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   15     1        ASSIGN                                                   !1, <array>
   16     2        ASSIGN                                                   !2, <array>
   18     3      > FE_RESET_R                                       $11     !0, ->23
          4    > > FE_FETCH_R                                               $11, !3, ->23
   20     5    >   FETCH_DIM_R                                      ~12     !3, 'type'
          6        ISSET_ISEMPTY_DIM_OBJ                         0  ~13     !2, ~12
          7        BOOL_NOT                                         ~14     ~13
          8      > JMPZ                                                     ~14, ->15
   21     9    >   FETCH_DIM_R                                      ~15     !3, 'type'
         10        ASSIGN_DIM                                               !2, ~15
         11        OP_DATA                                                  0
   22    12        FETCH_DIM_R                                      ~17     !3, 'type'
         13        ASSIGN_DIM                                               !4, ~17
         14        OP_DATA                                                  <array>
   26    15    >   FETCH_DIM_R                                      ~19     !3, 'type'
         16        FETCH_DIM_RW                                     $20     !2, ~19
         17        PRE_INC                                                  $20
   29    18        FETCH_DIM_R                                      ~22     !3, 'type'
         19        FETCH_DIM_W                                      $23     !4, ~22
         20        ASSIGN_DIM                                               $23
         21        OP_DATA                                                  !3
   18    22      > JMP                                                      ->4
         23    >   FE_FREE                                                  $11
   33    24        INIT_FCALL                                               'arsort'
         25        SEND_REF                                                 !2
         26        SEND_VAL                                                 1
         27        DO_ICALL                                                 
   36    28        INIT_FCALL                                               'array_keys'
         29        SEND_VAR                                                 !2
         30        DO_ICALL                                         $26     
         31        ASSIGN                                                   !5, $26
   39    32        ASSIGN                                                   !6, 0
   43    33      > JMP                                                      ->50
   44    34    >   FETCH_DIM_R                                      ~29     !5, !6
         35        ASSIGN                                                   !7, ~29
   47    36        FETCH_DIM_R                                      ~31     !2, !7
         37      > JMPZ                                                     ~31, ->46
   49    38    >   INIT_FCALL                                               'array_pop'
         39        FETCH_DIM_W                                      $33     !4, !7
         40        SEND_REF                                                 $33
         41        DO_ICALL                                         $34     
         42        ASSIGN_DIM                                               !1
         43        OP_DATA                                                  $34
   52    44        FETCH_DIM_RW                                     $35     !2, !7
         45        PRE_DEC                                                  $35
   56    46    >   PRE_INC                                          ~37     !6
         47        COUNT                                            ~38     !5
         48        MOD                                              ~39     ~37, ~38
         49        ASSIGN                                                   !6, ~39
   43    50    >   COUNT                                            ~41     !1
         51        COUNT                                            ~42     !0
         52        IS_SMALLER                                               ~41, ~42
         53      > JMPNZ                                                    ~43, ->34
   59    54    >   INIT_FCALL                                               'print_r'
         55        SEND_VAR                                                 !1
         56        DO_ICALL                                                 
         57      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.64 ms | 1400 KiB | 21 Q