3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sanitizeColumnReference($row, $columnReference) { if (!isset($row[$columnReference]) && is_int($columnReference)) { $columnReference = array_keys($row)[$columnReference] ?? null; // attempt to derive column name by position if ($columnReference === null) { throw new Exception('Failed to locate column by position using column reference: ' . $columnReference); } } return $columnReference; } function dynamicSort(&$input, $sortingRules) { if (!$input || !$sortingRules || !is_array($input) || !is_array($sortingRules)) { return; // return silently } $firstRow = current($input); $sortingParams = []; foreach ($sortingRules as $rule) { $rule = (array)$rule; // permit the passing of a solitary string as a sorting rule $columnReference = sanitizeColumnReference($firstRow, array_shift($rule)); $column = array_column($input, $columnReference); if (!$column) { throw new Exception('Failed to source sortable data from column reference: ' . $columnReference); } $sortingParams[] = $column; foreach ($rule as $flag) { $sortingParams[] = constant('SORT_' . strtoupper($flag)); // convert strings to usable CONSTANTs } } $sortingParams[] = &$input; var_export($sortingParams); array_multisort(...$sortingParams); // unpack into native sorting function } $array = [ ['first_name' => 'Homer', 'last_name' => 'Simpson', 'city' => 'Springfield', 'state' => 'Unknown', 'zip' => '66735'], ['first_name' => 'Patty', 'last_name' => 'Bouvier', 'city' => 'Scottsdale', 'state' => 'Arizona', 'zip' => '85250'], ['first_name' => 'Moe', 'last_name' => 'Szyslak', 'city' => 'Scottsdale', 'state' => 'Arizona', 'zip' => '85255'], ['first_name' => 'Nick', 'last_name' => 'Riviera', 'city' => 'Scottsdale', 'state' => 'Arizona', 'zip' => '85255'], ]; $sortingRules = [ 'city', ['zip', 'desc', 'numeric'], [1, 'desc'], // last_name ]; echo "Sorting Params:\n"; dynamicSort($array, $sortingRules); // this modifies by reference like native sorting functions echo "\n---\nOutput:\n"; var_export($array);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JKhtR
function name:  (null)
number of ops:  12
compiled vars:  !0 = $array, !1 = $sortingRules
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   ASSIGN                                                   !0, <array>
   43     1        ASSIGN                                                   !1, <array>
   49     2        ECHO                                                     'Sorting+Params%3A%0A'
   51     3        INIT_FCALL                                               'dynamicsort'
          4        SEND_REF                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0          
   53     7        ECHO                                                     '%0A---%0AOutput%3A%0A'
   55     8        INIT_FCALL                                               'var_export'
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                                 
         11      > RETURN                                                   1

Function sanitizecolumnreference:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 22
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
Branch analysis from position: 7
filename:       /in/JKhtR
function name:  sanitizeColumnReference
number of ops:  24
compiled vars:  !0 = $row, !1 = $columnReference
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      !0, !1
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ_EX                                          ~3      ~3, ->7
          5    >   TYPE_CHECK                                   16  ~4      !1
          6        BOOL                                             ~3      ~4
          7    > > JMPZ                                                     ~3, ->22
    5     8    >   INIT_FCALL                                               'array_keys'
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                         $5      
         11        FETCH_DIM_IS                                     ~6      $5, !1
         12        COALESCE                                         ~7      ~6
         13        QM_ASSIGN                                        ~7      null
         14        ASSIGN                                                   !1, ~7
    6    15        TYPE_CHECK                                    2          !1
         16      > JMPZ                                                     ~9, ->22
    7    17    >   NEW                                              $10     'Exception'
         18        CONCAT                                           ~11     'Failed+to+locate+column+by+position+using+column+reference%3A+', !1
         19        SEND_VAL_EX                                              ~11
         20        DO_FCALL                                      0          
         21      > THROW                                         0          $10
   10    22    > > RETURN                                                   !1
   11    23*     > RETURN                                                   null

End of function sanitizecolumnreference

Function dynamicsort:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
2 jumps found. (Code = 47) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 61
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 61
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 45
Branch analysis from position: 40
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 45
2 jumps found. (Code = 77) Position 1 = 48, Position 2 = 59
Branch analysis from position: 48
2 jumps found. (Code = 78) Position 1 = 49, Position 2 = 59
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 59
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 61
Branch analysis from position: 14
Branch analysis from position: 10
Branch analysis from position: 6
filename:       /in/JKhtR
function name:  dynamicSort
number of ops:  72
compiled vars:  !0 = $input, !1 = $sortingRules, !2 = $firstRow, !3 = $sortingParams, !4 = $rule, !5 = $columnReference, !6 = $column, !7 = $flag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        BOOL_NOT                                         ~8      !0
          3      > JMPNZ_EX                                         ~8      ~8, ->6
          4    >   BOOL_NOT                                         ~9      !1
          5        BOOL                                             ~8      ~9
          6    > > JMPNZ_EX                                         ~8      ~8, ->10
          7    >   TYPE_CHECK                                  128  ~10     !0
          8        BOOL_NOT                                         ~11     ~10
          9        BOOL                                             ~8      ~11
         10    > > JMPNZ_EX                                         ~8      ~8, ->14
         11    >   TYPE_CHECK                                  128  ~12     !1
         12        BOOL_NOT                                         ~13     ~12
         13        BOOL                                             ~8      ~13
         14    > > JMPZ                                                     ~8, ->16
   15    15    > > RETURN                                                   null
   17    16    >   INIT_FCALL                                               'current'
         17        SEND_VAR                                                 !0
         18        DO_ICALL                                         $14     
         19        ASSIGN                                                   !2, $14
   18    20        ASSIGN                                                   !3, <array>
   19    21      > FE_RESET_R                                       $17     !1, ->61
         22    > > FE_FETCH_R                                               $17, !4, ->61
   20    23    >   CAST                                          7  ~18     !4
         24        ASSIGN                                                   !4, ~18
   21    25        INIT_FCALL                                               'sanitizecolumnreference'
         26        SEND_VAR                                                 !2
         27        INIT_FCALL                                               'array_shift'
         28        SEND_REF                                                 !4
         29        DO_ICALL                                         $20     
         30        SEND_VAR                                                 $20
         31        DO_FCALL                                      0  $21     
         32        ASSIGN                                                   !5, $21
   22    33        INIT_FCALL                                               'array_column'
         34        SEND_VAR                                                 !0
         35        SEND_VAR                                                 !5
         36        DO_ICALL                                         $23     
         37        ASSIGN                                                   !6, $23
   23    38        BOOL_NOT                                         ~25     !6
         39      > JMPZ                                                     ~25, ->45
   24    40    >   NEW                                              $26     'Exception'
         41        CONCAT                                           ~27     'Failed+to+source+sortable+data+from+column+reference%3A+', !5
         42        SEND_VAL_EX                                              ~27
         43        DO_FCALL                                      0          
         44      > THROW                                         0          $26
   26    45    >   ASSIGN_DIM                                               !3
         46        OP_DATA                                                  !6
   27    47      > FE_RESET_R                                       $30     !4, ->59
         48    > > FE_FETCH_R                                               $30, !7, ->59
   28    49    >   INIT_FCALL                                               'constant'
         50        INIT_FCALL                                               'strtoupper'
         51        SEND_VAR                                                 !7
         52        DO_ICALL                                         $32     
         53        CONCAT                                           ~33     'SORT_', $32
         54        SEND_VAL                                                 ~33
         55        DO_ICALL                                         $34     
         56        ASSIGN_DIM                                               !3
         57        OP_DATA                                                  $34
   27    58      > JMP                                                      ->48
         59    >   FE_FREE                                                  $30
   19    60      > JMP                                                      ->22
         61    >   FE_FREE                                                  $17
   31    62        FETCH_DIM_W                                      $35     !3
         63        ASSIGN_REF                                               $35, !0
   32    64        INIT_FCALL                                               'var_export'
         65        SEND_VAR                                                 !3
         66        DO_ICALL                                                 
   33    67        INIT_FCALL                                               'array_multisort'
         68        SEND_UNPACK                                              !3
         69        CHECK_UNDEF_ARGS                                         
         70        DO_ICALL                                                 
   34    71      > RETURN                                                   null

End of function dynamicsort

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
229.07 ms | 1019 KiB | 23 Q