3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sort_by_key($key) { return fn($a, $b) => strnatcmp($a[$key], $b[$key]); } $array = [ [ 'id' => 3, 'username' => 'TML', 'realname' => 'Joey' ], [ 'id' => 1, 'username' => '__adrian', 'realname' => 'Adrian, presumably?' ], [ 'id' => 2, 'username' => 'Blondie101010', 'realname' => 'No idea' ] ]; foreach(array_keys($array[0]) as $key) { echo "\n\nSorting array by key {$key}:\n"; usort($array, sort_by_key($key)); var_dump($array); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 22
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 22
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/2jjjE
function name:  (null)
number of ops:  24
compiled vars:  !0 = $array, !1 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   ASSIGN                                                   !0, <array>
   23     1        INIT_FCALL                                               'array_keys'
          2        FETCH_DIM_R                                      ~3      !0, 0
          3        SEND_VAL                                                 ~3
          4        DO_ICALL                                         $4      
          5      > FE_RESET_R                                       $5      $4, ->22
          6    > > FE_FETCH_R                                               $5, !1, ->22
   24     7    >   ROPE_INIT                                     3  ~7      '%0A%0ASorting+array+by+key+'
          8        ROPE_ADD                                      1  ~7      ~7, !1
          9        ROPE_END                                      2  ~6      ~7, '%3A%0A'
         10        ECHO                                                     ~6
   25    11        INIT_FCALL                                               'usort'
         12        SEND_REF                                                 !0
         13        INIT_FCALL                                               'sort_by_key'
         14        SEND_VAR                                                 !1
         15        DO_FCALL                                      0  $9      
         16        SEND_VAR                                                 $9
         17        DO_ICALL                                                 
   26    18        INIT_FCALL                                               'var_dump'
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                                 
   23    21      > JMP                                                      ->6
         22    >   FE_FREE                                                  $5
   27    23      > RETURN                                                   1

Function sort_by_key:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2jjjE
function name:  sort_by_key
number of ops:  5
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F2jjjE%3A4%240'
          2        BIND_LEXICAL                                             ~1, !0
          3      > RETURN                                                   ~1
    5     4*     > RETURN                                                   null

End of function sort_by_key

Function %00%7Bclosure%7D%2Fin%2F2jjjE%3A4%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2jjjE
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $a, !1 = $b, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        INIT_FCALL                                               'strnatcmp'
          4        FETCH_DIM_R                                      ~3      !0, !2
          5        SEND_VAL                                                 ~3
          6        FETCH_DIM_R                                      ~4      !1, !2
          7        SEND_VAL                                                 ~4
          8        DO_ICALL                                         $5      
          9      > RETURN                                                   $5
         10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F2jjjE%3A4%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.3 ms | 1407 KiB | 22 Q