3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sortBy ( $arr, $sortKey ) { /*Временный массив для сотритовки по нужному ключу*/ $tempArr = array(); /*Возвращаемый массив*/ $returnArr = array(); /*Перебор всего массива*/ foreach ( $arr AS $key=>$value ) { /*Запись в массив значений ключа по которому быдет выполнена сортировка*/ $tempArr[$key] = $value[$sortKey]; } /*Сортировка значений с сохранением ключей*/ asort ( $tempArr ); /*Установка указателя массива не первый элемент*/ reset ( $tempArr ); /*Перебор всего временного массива*/ foreach ( $tempArr AS $key=>$value ) { /*Формирование возвращаемого массива*/ $returnArr[] = $arr[$key]; } return $returnArr; } function sortByObj ( $arr, $sortKey ) { /*Временный массив для сотритовки по нужному ключу*/ $tempArr = array(); /*Возвращаемый массив*/ $returnArr = array(); /*Перебор всего массива*/ foreach ( $arr AS $key=>$value ) { /*Запись в массив значений ключа по которому быдет выполнена сортировка*/ $tempArr[$key] = $value->{$sortKey}; } /*Сортировка значений с сохранением ключей*/ asort ( $tempArr ); /*Установка указателя массива не первый элемент*/ reset ( $tempArr ); /*Перебор всего временного массива*/ foreach ( $tempArr AS $key=>$value ) { /*Формирование возвращаемого массива*/ $returnArr[] = $arr[$key]; } return $returnArr; } /*Ключ по которому будет выполнена сортировка*/ $sortKey = "name"; /*Второй ключ присутствующий в массиве*/ $secondKey = "data"; $arr[1]['data'] = "2010"; $arr[1]['name'] = "Петя"; $arr[2]['data'] = "2012"; $arr[2]['name'] = "Вася"; $arr[3]['data'] = "2011"; $arr[3]['name'] = "Саша"; $ar = array(); $obj = new stdClass(); $obj->ID = 1; $obj->Name = 'Дискета'; $ar[] = $obj; $obj = new stdClass(); $obj->ID = 3; $obj->Name = 'E-token'; $ar[] = $obj; $obj = new stdClass(); $obj->ID = 2; $obj->Name = 'Rutoken'; $ar[] = $obj; $obj = new stdClass(); $obj->ID = 4; $obj->Name = 'Флеш накопитель'; $ar[] = $obj; $sortName = 'Name'; $sf = 'ID'; /* [0] => stdClass Object ( [ID] => 1 [Name] => Дискета ) [1] => stdClass Object ( [ID] => 3 [Name] => E-token ) [2] => stdClass Object ( [ID] => 2 [Name] => Rutoken ) [3] => stdClass Object ( [ID] => 4 [Name] => Флеш накопитель ) */ $sortedArr = sortBy( $arr, $sortKey ); foreach ( $sortedArr AS $key=>$value ) { echo $key.' = '. $value[$sortKey] . " - " . $value[$secondKey] . "<br />"; } echo('<br>'."\n\n"); $sArr = sortBy( $ar, $sortName ); print_r($sArr);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 65, Position 2 = 76
Branch analysis from position: 65
2 jumps found. (Code = 78) Position 1 = 66, Position 2 = 76
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
Branch analysis from position: 76
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 76
filename:       /in/vSG0c
function name:  (null)
number of ops:  87
compiled vars:  !0 = $sortKey, !1 = $secondKey, !2 = $arr, !3 = $ar, !4 = $obj, !5 = $sortName, !6 = $sf, !7 = $sortedArr, !8 = $value, !9 = $key, !10 = $sArr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   ASSIGN                                                   !0, 'name'
   58     1        ASSIGN                                                   !1, 'data'
   60     2        FETCH_DIM_W                                      $13     !2, 1
          3        ASSIGN_DIM                                               $13, 'data'
          4        OP_DATA                                                  '2010'
   61     5        FETCH_DIM_W                                      $15     !2, 1
          6        ASSIGN_DIM                                               $15, 'name'
          7        OP_DATA                                                  '%D0%9F%D0%B5%D1%82%D1%8F'
   62     8        FETCH_DIM_W                                      $17     !2, 2
          9        ASSIGN_DIM                                               $17, 'data'
         10        OP_DATA                                                  '2012'
   63    11        FETCH_DIM_W                                      $19     !2, 2
         12        ASSIGN_DIM                                               $19, 'name'
         13        OP_DATA                                                  '%D0%92%D0%B0%D1%81%D1%8F'
   64    14        FETCH_DIM_W                                      $21     !2, 3
         15        ASSIGN_DIM                                               $21, 'data'
         16        OP_DATA                                                  '2011'
   65    17        FETCH_DIM_W                                      $23     !2, 3
         18        ASSIGN_DIM                                               $23, 'name'
         19        OP_DATA                                                  '%D0%A1%D0%B0%D1%88%D0%B0'
   67    20        ASSIGN                                                   !3, <array>
   69    21        NEW                                              $26     'stdClass'
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !4, $26
   70    24        ASSIGN_OBJ                                               !4, 'ID'
         25        OP_DATA                                                  1
   71    26        ASSIGN_OBJ                                               !4, 'Name'
         27        OP_DATA                                                  '%D0%94%D0%B8%D1%81%D0%BA%D0%B5%D1%82%D0%B0'
   72    28        ASSIGN_DIM                                               !3
         29        OP_DATA                                                  !4
   74    30        NEW                                              $32     'stdClass'
         31        DO_FCALL                                      0          
         32        ASSIGN                                                   !4, $32
   75    33        ASSIGN_OBJ                                               !4, 'ID'
         34        OP_DATA                                                  3
   76    35        ASSIGN_OBJ                                               !4, 'Name'
         36        OP_DATA                                                  'E-token'
   77    37        ASSIGN_DIM                                               !3
         38        OP_DATA                                                  !4
   79    39        NEW                                              $38     'stdClass'
         40        DO_FCALL                                      0          
         41        ASSIGN                                                   !4, $38
   80    42        ASSIGN_OBJ                                               !4, 'ID'
         43        OP_DATA                                                  2
   81    44        ASSIGN_OBJ                                               !4, 'Name'
         45        OP_DATA                                                  'Rutoken'
   82    46        ASSIGN_DIM                                               !3
         47        OP_DATA                                                  !4
   84    48        NEW                                              $44     'stdClass'
         49        DO_FCALL                                      0          
         50        ASSIGN                                                   !4, $44
   85    51        ASSIGN_OBJ                                               !4, 'ID'
         52        OP_DATA                                                  4
   86    53        ASSIGN_OBJ                                               !4, 'Name'
         54        OP_DATA                                                  '%D0%A4%D0%BB%D0%B5%D1%88+%D0%BD%D0%B0%D0%BA%D0%BE%D0%BF%D0%B8%D1%82%D0%B5%D0%BB%D1%8C'
   87    55        ASSIGN_DIM                                               !3
         56        OP_DATA                                                  !4
   89    57        ASSIGN                                                   !5, 'Name'
   90    58        ASSIGN                                                   !6, 'ID'
  120    59        INIT_FCALL                                               'sortby'
         60        SEND_VAR                                                 !2
         61        SEND_VAR                                                 !0
         62        DO_FCALL                                      0  $52     
         63        ASSIGN                                                   !7, $52
  122    64      > FE_RESET_R                                       $54     !7, ->76
         65    > > FE_FETCH_R                                       ~55     $54, !8, ->76
         66    >   ASSIGN                                                   !9, ~55
  124    67        CONCAT                                           ~57     !9, '+%3D+'
         68        FETCH_DIM_R                                      ~58     !8, !0
         69        CONCAT                                           ~59     ~57, ~58
         70        CONCAT                                           ~60     ~59, '+-+'
         71        FETCH_DIM_R                                      ~61     !8, !1
         72        CONCAT                                           ~62     ~60, ~61
         73        CONCAT                                           ~63     ~62, '%3Cbr+%2F%3E'
         74        ECHO                                                     ~63
  122    75      > JMP                                                      ->65
         76    >   FE_FREE                                                  $54
  126    77        ECHO                                                     '%3Cbr%3E%0A%0A'
  128    78        INIT_FCALL                                               'sortby'
         79        SEND_VAR                                                 !3
         80        SEND_VAR                                                 !5
         81        DO_FCALL                                      0  $64     
         82        ASSIGN                                                   !10, $64
  129    83        INIT_FCALL                                               'print_r'
         84        SEND_VAR                                                 !10
         85        DO_ICALL                                                 
         86      > RETURN                                                   1

Function sortby:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 25
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Branch analysis from position: 11
filename:       /in/vSG0c
function name:  sortBy
number of ops:  28
compiled vars:  !0 = $arr, !1 = $sortKey, !2 = $tempArr, !3 = $returnArr, !4 = $value, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        ASSIGN                                                   !2, <array>
    8     3        ASSIGN                                                   !3, <array>
   11     4      > FE_RESET_R                                       $8      !0, ->11
          5    > > FE_FETCH_R                                       ~9      $8, !4, ->11
          6    >   ASSIGN                                                   !5, ~9
   14     7        FETCH_DIM_R                                      ~12     !4, !1
          8        ASSIGN_DIM                                               !2, !5
          9        OP_DATA                                                  ~12
   11    10      > JMP                                                      ->5
         11    >   FE_FREE                                                  $8
   17    12        INIT_FCALL                                               'asort'
         13        SEND_REF                                                 !2
         14        DO_ICALL                                                 
   19    15        INIT_FCALL                                               'reset'
         16        SEND_REF                                                 !2
         17        DO_ICALL                                                 
   21    18      > FE_RESET_R                                       $15     !2, ->25
         19    > > FE_FETCH_R                                       ~16     $15, !4, ->25
         20    >   ASSIGN                                                   !5, ~16
   24    21        FETCH_DIM_R                                      ~19     !0, !5
         22        ASSIGN_DIM                                               !3
         23        OP_DATA                                                  ~19
   21    24      > JMP                                                      ->19
         25    >   FE_FREE                                                  $15
   26    26      > RETURN                                                   !3
   27    27*     > RETURN                                                   null

End of function sortby

Function sortbyobj:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 25
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Branch analysis from position: 11
filename:       /in/vSG0c
function name:  sortByObj
number of ops:  28
compiled vars:  !0 = $arr, !1 = $sortKey, !2 = $tempArr, !3 = $returnArr, !4 = $value, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   32     2        ASSIGN                                                   !2, <array>
   34     3        ASSIGN                                                   !3, <array>
   37     4      > FE_RESET_R                                       $8      !0, ->11
          5    > > FE_FETCH_R                                       ~9      $8, !4, ->11
          6    >   ASSIGN                                                   !5, ~9
   40     7        FETCH_OBJ_R                                      ~12     !4, !1
          8        ASSIGN_DIM                                               !2, !5
          9        OP_DATA                                                  ~12
   37    10      > JMP                                                      ->5
         11    >   FE_FREE                                                  $8
   43    12        INIT_FCALL                                               'asort'
         13        SEND_REF                                                 !2
         14        DO_ICALL                                                 
   45    15        INIT_FCALL                                               'reset'
         16        SEND_REF                                                 !2
         17        DO_ICALL                                                 
   47    18      > FE_RESET_R                                       $15     !2, ->25
         19    > > FE_FETCH_R                                       ~16     $15, !4, ->25
         20    >   ASSIGN                                                   !5, ~16
   50    21        FETCH_DIM_R                                      ~19     !0, !5
         22        ASSIGN_DIM                                               !3
         23        OP_DATA                                                  ~19
   47    24      > JMP                                                      ->19
         25    >   FE_FREE                                                  $15
   52    26      > RETURN                                                   !3
   53    27*     > RETURN                                                   null

End of function sortbyobj

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.76 ms | 1410 KiB | 21 Q