3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ ['id' => 1, 'user' => ['rating' => ['top' => 50, 'avg' => 30]]], ['id' => 2, 'user' => ['rating' => ['top' => 60, 'avg' => 10]]], ['id' => 3, 'user' => ['rating' => ['top' => 25, 'avg' => 12]]], ['id' => 4, 'user' => ['rating' => ['top' => 30, 'avg' => 20]]], ['id' => 5, 'user' => ['rating' => ['top' => 30, 'avg' => 5]]], ]; shuffle($data); function sortBy(&$array, $field) { $accessor = function($row, $path) { $steps = explode('.', $path); $return = $row[array_shift($steps)]; while ($level = array_shift($steps)) { $return =& $return[$level]; } return $return; }; usort($array, function ($a, $b) use ($field, $accessor) { $aVal = $accessor($a, $field); $bVal = $accessor($b, $field); return $aVal <=> $bVal; }); } sortBy($data, 'id'); print_r($data); sortBy($data, 'user.rating.top'); print_r($data); sortBy($data, 'user.rating.avg'); print_r($data);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UciGc
function name:  (null)
number of ops:  26
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   10     1        INIT_FCALL                                               'shuffle'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                                 
   33     4        INIT_FCALL                                               'sortby'
          5        SEND_REF                                                 !0
          6        SEND_VAL                                                 'id'
          7        DO_FCALL                                      0          
   34     8        INIT_FCALL                                               'print_r'
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                                 
   36    11        INIT_FCALL                                               'sortby'
         12        SEND_REF                                                 !0
         13        SEND_VAL                                                 'user.rating.top'
         14        DO_FCALL                                      0          
   37    15        INIT_FCALL                                               'print_r'
         16        SEND_VAR                                                 !0
         17        DO_ICALL                                                 
   39    18        INIT_FCALL                                               'sortby'
         19        SEND_REF                                                 !0
         20        SEND_VAL                                                 'user.rating.avg'
         21        DO_FCALL                                      0          
   40    22        INIT_FCALL                                               'print_r'
         23        SEND_VAR                                                 !0
         24        DO_ICALL                                                 
         25      > RETURN                                                   1

Function sortby:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UciGc
function name:  sortBy
number of ops:  12
compiled vars:  !0 = $array, !1 = $field, !2 = $accessor
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   13     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FUciGc%3A13%240'
          3        ASSIGN                                                   !2, ~3
   25     4        INIT_FCALL                                               'usort'
          5        SEND_REF                                                 !0
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FUciGc%3A25%241'
          7        BIND_LEXICAL                                             ~5, !1
          8        BIND_LEXICAL                                             ~5, !2
   30     9        SEND_VAL                                                 ~5
         10        DO_ICALL                                                 
   31    11      > RETURN                                                   null

End of function sortby

Function %00%7Bclosure%7D%2Fin%2FUciGc%3A13%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 13
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 13
Branch analysis from position: 20
Branch analysis from position: 13
filename:       /in/UciGc
function name:  {closure}
number of ops:  22
compiled vars:  !0 = $row, !1 = $path, !2 = $steps, !3 = $return, !4 = $level
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '.'
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $5      
          6        ASSIGN                                                   !2, $5
   16     7        INIT_FCALL                                               'array_shift'
          8        SEND_REF                                                 !2
          9        DO_ICALL                                         $7      
         10        FETCH_DIM_R                                      ~8      !0, $7
         11        ASSIGN                                                   !3, ~8
   18    12      > JMP                                                      ->15
   19    13    >   FETCH_DIM_W                                      $10     !3, !4
         14        ASSIGN_REF                                               !3, $10
   18    15    >   INIT_FCALL                                               'array_shift'
         16        SEND_REF                                                 !2
         17        DO_ICALL                                         $12     
         18        ASSIGN                                           ~13     !4, $12
         19      > JMPNZ                                                    ~13, ->13
   22    20    > > RETURN                                                   !3
   23    21*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FUciGc%3A13%240

Function %00%7Bclosure%7D%2Fin%2FUciGc%3A25%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UciGc
function name:  {closure}
number of ops:  17
compiled vars:  !0 = $a, !1 = $b, !2 = $field, !3 = $accessor, !4 = $aVal, !5 = $bVal
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
   26     4        INIT_DYNAMIC_CALL                                        !3
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !4, $6
   27     9        INIT_DYNAMIC_CALL                                        !3
         10        SEND_VAR_EX                                              !1
         11        SEND_VAR_EX                                              !2
         12        DO_FCALL                                      0  $8      
         13        ASSIGN                                                   !5, $8
   29    14        SPACESHIP                                        ~10     !4, !5
         15      > RETURN                                                   ~10
   30    16*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FUciGc%3A25%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.91 ms | 1407 KiB | 26 Q