3v4l.org

run code in 300+ PHP versions simultaneously
<?php // groupA: // James: // gpa: 3 // age: 13 // Charles: // gpa: 4 // age: 15 // John: // gpa: 2 // age: 17 $groupA = [ 'James' => [ 'gpa' => 3, 'age' => 13 ], 'Charles' => [ 'gpa' => 4, 'age' => 15 ], 'John' => [ 'gpa' => 2, 'age' => 17 ] ]; /** * @throws \InvalidArgumentException * @throws \RuntimeException */ function getPlace(string $for, string $compareValueKey, array $haystack, int $flags = SORT_REGULAR): int { if(empty($haystack) || !isset($haystack[$for])) { throw new \InvalidArgumentException("empty haystack array given (or key '$for') was not found"); } $sort = array_map(fn(array $el) => $el[$compareValueKey], $haystack); arsort($sort, $flags); $place = array_search($for, $places = array_keys($sort), true); if(!is_int($place)) { throw new \RuntimeException("could not locate position for key '$for' in given array: " . trim(implode(', ', $places))); } return $place + 1; } $persons = array_keys($groupA); foreach($persons as $person) { echo $person . ': ' . getPlace($person, 'age', $groupA) . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 17
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 17
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/JIbOv
function name:  (null)
number of ops:  19
compiled vars:  !0 = $groupA, !1 = $persons, !2 = $person
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   ASSIGN                                                   !0, <array>
   53     1        INIT_FCALL                                               'array_keys'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4        ASSIGN                                                   !1, $4
   55     5      > FE_RESET_R                                       $6      !1, ->17
          6    > > FE_FETCH_R                                               $6, !2, ->17
   57     7    >   CONCAT                                           ~7      !2, '%3A+'
          8        INIT_FCALL                                               'getplace'
          9        SEND_VAR                                                 !2
         10        SEND_VAL                                                 'age'
         11        SEND_VAR                                                 !0
         12        DO_FCALL                                      0  $8      
         13        CONCAT                                           ~9      ~7, $8
         14        CONCAT                                           ~10     ~9, '%0A'
         15        ECHO                                                     ~10
   55    16      > JMP                                                      ->6
         17    >   FE_FREE                                                  $6
   58    18      > RETURN                                                   1

Function getplace:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 17
Branch analysis from position: 10
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 56
Branch analysis from position: 41
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/JIbOv
function name:  getPlace
number of ops:  61
compiled vars:  !0 = $for, !1 = $compareValueKey, !2 = $haystack, !3 = $flags, !4 = $sort, !5 = $place, !6 = $places
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      <const ast>
   35     4        ISSET_ISEMPTY_CV                                 ~7      !2
          5      > JMPNZ_EX                                         ~7      ~7, ->9
          6    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~8      !2, !0
          7        BOOL_NOT                                         ~9      ~8
          8        BOOL                                             ~7      ~9
          9    > > JMPZ                                                     ~7, ->17
   37    10    >   NEW                                              $10     'InvalidArgumentException'
         11        ROPE_INIT                                     3  ~12     'empty+haystack+array+given+%28or+key+%27'
         12        ROPE_ADD                                      1  ~12     ~12, !0
         13        ROPE_END                                      2  ~11     ~12, '%27%29+was+not+found'
         14        SEND_VAL_EX                                              ~11
         15        DO_FCALL                                      0          
         16      > THROW                                         0          $10
   40    17    >   INIT_FCALL                                               'array_map'
         18        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FJIbOv%3A40%240'
         19        BIND_LEXICAL                                             ~15, !1
         20        SEND_VAL                                                 ~15
         21        SEND_VAR                                                 !2
         22        DO_ICALL                                         $16     
         23        ASSIGN                                                   !4, $16
   41    24        INIT_FCALL                                               'arsort'
         25        SEND_REF                                                 !4
         26        SEND_VAR                                                 !3
         27        DO_ICALL                                                 
   43    28        INIT_FCALL                                               'array_search'
         29        SEND_VAR                                                 !0
         30        INIT_FCALL                                               'array_keys'
         31        SEND_VAR                                                 !4
         32        DO_ICALL                                         $19     
         33        ASSIGN                                           ~20     !6, $19
         34        SEND_VAL                                                 ~20
         35        SEND_VAL                                                 <true>
         36        DO_ICALL                                         $21     
         37        ASSIGN                                                   !5, $21
   45    38        TYPE_CHECK                                   16  ~23     !5
         39        BOOL_NOT                                         ~24     ~23
         40      > JMPZ                                                     ~24, ->56
   47    41    >   NEW                                              $25     'RuntimeException'
         42        ROPE_INIT                                     3  ~27     'could+not+locate+position+for+key+%27'
         43        ROPE_ADD                                      1  ~27     ~27, !0
         44        ROPE_END                                      2  ~26     ~27, '%27+in+given+array%3A+'
         45        INIT_FCALL                                               'trim'
         46        INIT_FCALL                                               'implode'
         47        SEND_VAL                                                 '%2C+'
         48        SEND_VAR                                                 !6
         49        DO_ICALL                                         $29     
         50        SEND_VAR                                                 $29
         51        DO_ICALL                                         $30     
         52        CONCAT                                           ~31     ~26, $30
         53        SEND_VAL_EX                                              ~31
         54        DO_FCALL                                      0          
         55      > THROW                                         0          $25
   50    56    >   ADD                                              ~33     !5, 1
         57        VERIFY_RETURN_TYPE                                       ~33
         58      > RETURN                                                   ~33
   51    59*       VERIFY_RETURN_TYPE                                       
         60*     > RETURN                                                   null

End of function getplace

Function %00%7Bclosure%7D%2Fin%2FJIbOv%3A40%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JIbOv
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $el, !1 = $compareValueKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_DIM_R                                      ~2      !0, !1
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FJIbOv%3A40%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.8 ms | 1411 KiB | 26 Q