3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ["name" => "a", "gender" => "male"], ["name" => "b", "gender" => "female"], ["name" => "c", "gender" => "female"], ["name" => "d", "gender" => "female"], ["name" => "e", "gender" => "male"], ["name" => "f", "gender" => "male"], ["name" => "g", "gender" => "male"], ]; $nths = []; $genders = []; foreach ($array as ['gender' => $v]) { $genders[] = $v; $nths[] = $counters[$v] = ($counters[$v] ?? 0) + 1; } array_multisort( $nths, /* prioritize by order of encounter per unique gender */ $genders, /* sort by gender per encounter group */ SORT_DESC, /* sort "male" before "female" per encounter group */ $array /* break any remaining ties and modify the original array */ ); var_export($array);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 19
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 19
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/FAm3G
function name:  (null)
number of ops:  30
compiled vars:  !0 = $array, !1 = $nths, !2 = $genders, !3 = $v, !4 = $counters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   13     1        ASSIGN                                                   !1, <array>
   14     2        ASSIGN                                                   !2, <array>
   15     3      > FE_RESET_R                                       $8      !0, ->19
          4    > > FE_FETCH_R                                               $8, $9, ->19
          5    >   FETCH_LIST_R                                     $10     $9, 'gender'
          6        ASSIGN                                                   !3, $10
          7        FREE                                                     $9
   16     8        ASSIGN_DIM                                               !2
          9        OP_DATA                                                  !3
   17    10        FETCH_DIM_IS                                     ~15     !4, !3
         11        COALESCE                                         ~16     ~15
         12        QM_ASSIGN                                        ~16     0
         13        ADD                                              ~17     ~16, 1
         14        ASSIGN_DIM                                       ~14     !4, !3
         15        OP_DATA                                                  ~17
         16        ASSIGN_DIM                                               !1
         17        OP_DATA                                                  ~14
   15    18      > JMP                                                      ->4
         19    >   FE_FREE                                                  $8
   20    20        INIT_FCALL                                               'array_multisort'
   21    21        SEND_REF                                                 !1
   22    22        SEND_REF                                                 !2
   23    23        SEND_VAL                                                 3
   24    24        SEND_REF                                                 !0
   20    25        DO_ICALL                                                 
   26    26        INIT_FCALL                                               'var_export'
         27        SEND_VAR                                                 !0
         28        DO_ICALL                                                 
         29      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.64 ms | 1012 KiB | 15 Q