3v4l.org

run code in 500+ PHP versions simultaneously
<?php $students = [ ["name" => "...", "gender" => "male"], ["name" => "...", "gender" => "female"], ["name" => "...", "gender" => "female"], ["name" => "...", "gender" => "female"], ["name" => "...", "gender" => "male"], ["name" => "...", "gender" => "male"], ["name" => "...", "gender" => "male"], ]; $counters = ['female' => 1, 'male' => 1]; // determine which gender should start from 0 $genderCounts = array_count_values( array_column($students, 'gender') ); arsort($genderCounts); --$counters[key($genderCounts)]; // assign keys $result = []; foreach ($students as $student) { $gender = $student['gender']; $result[$counters[$gender]] = $student; $counters[$gender] += 2; } ksort($result); var_export($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 20, Position 2 = 29
Branch analysis from position: 20
2 jumps found. (Code = 78) Position 1 = 21, Position 2 = 29
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/005Wc
function name:  (null)
number of ops:  37
compiled vars:  !0 = $students, !1 = $counters, !2 = $genderCounts, !3 = $result, !4 = $student, !5 = $gender
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   13     1        ASSIGN                                                       !1, <array>
   16     2        INIT_FCALL                                                   'array_count_values'
   17     3        INIT_FCALL                                                   'array_column'
          4        SEND_VAR                                                     !0
          5        SEND_VAL                                                     'gender'
          6        DO_ICALL                                             $8      
          7        SEND_VAR                                                     $8
   16     8        DO_ICALL                                             $9      
          9        ASSIGN                                                       !2, $9
   19    10        INIT_FCALL                                                   'arsort'
         11        SEND_REF                                                     !2
         12        DO_ICALL                                                     
   20    13        INIT_FCALL                                                   'key'
         14        SEND_VAR                                                     !2
         15        DO_ICALL                                             $12     
         16        FETCH_DIM_RW                                         $13     !1, $12
         17        PRE_DEC                                                      $13
   23    18        ASSIGN                                                       !3, <array>
   24    19      > FE_RESET_R                                           $16     !0, ->29
         20    > > FE_FETCH_R                                                   $16, !4, ->29
   25    21    >   FETCH_DIM_R                                          ~17     !4, 'gender'
         22        ASSIGN                                                       !5, ~17
   26    23        FETCH_DIM_R                                          ~19     !1, !5
         24        ASSIGN_DIM                                                   !3, ~19
         25        OP_DATA                                                      !4
   27    26        ASSIGN_DIM_OP                    +=               1          !1, !5
         27        OP_DATA                                                      2
   24    28      > JMP                                                          ->20
         29    >   FE_FREE                                                      $16
   30    30        INIT_FCALL                                                   'ksort'
         31        SEND_REF                                                     !3
         32        DO_ICALL                                                     
   32    33        INIT_FCALL                                                   'var_export'
         34        SEND_VAR                                                     !3
         35        DO_ICALL                                                     
         36      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
178.51 ms | 2283 KiB | 19 Q