3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_rand_secure($array, $num = 1) { if (!is_array($array)) { trigger_error( __FUNCTION__ . "(): expects parameter 1 to be array, " . gettype($array) . " given", E_USER_WARNING ); return null; } $arrayLength = count($array); if ($arrayLength === 0) { trigger_error(__FUNCTION__ . "(): Array is empty", E_USER_WARNING); return null; } if ( filter_var($num, FILTER_VALIDATE_INT) === false || $num <= 0 || ($arrayLength - $num) < 0 ) { trigger_error( __FUNCTION__ . "(): Second argument has to be between 1 and the number of elements in the array", E_USER_WARNING); return null; } $randomIndexes = []; while(count($randomIndexes) < $num) { $randomInt = random_int(0, $arrayLength - 1); if (!in_array($randomInt, $randomIndexes)) $randomIndexes[] = $randomInt; }; $arrayKeys = array_keys($array); $randomKeys = array_map(function ($key) use ($arrayKeys) { return $arrayKeys[$key]; }, $randomIndexes); return count($randomKeys) === 1 ? $randomKeys[0] : $randomKeys; } $tests1 = [ [1, 2, 3, 4, 5, 6, 7, 8, 9], [5, 6, 7], ['a' => 1, 'b' => 2, 'c' => 3], ['zero', 4 => 'four', 9 => 'nine'], ["PEAN"=>0], [], "ABC", 123, ]; foreach ($tests1 as $test) { var_dump(array_rand_secure($test)); } $tests2 = [ ["array" => [1, 2, 3, 4, 5, 6, 7, 8, 9], "num" => 9], ["array" => [1, 2, 3, 4, 5, 6, 7, 8, 9], "num" => 4], ["array" => ['a' => 1, 'b' => 2, 'c' => 3], "num" => 2], ["array" => ['zero', 4 => 'four', 9 => 'nine'], "num" => 2], ["array" => ["PEAN" => 0], "num" => 2], ["array" => [], "num" => 2], ["array" => "ABC", "num" => 2], ["array" => 123, "num" => 2], ["array" => [5, 6, 7], "num" => 0], ]; foreach ($tests2 as $test_data) { var_dump(array_rand_secure($test_data["array"], $test_data["num"])); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 10
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 24
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 24
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
Branch analysis from position: 10
filename:       /in/HGnAk
function name:  (null)
number of ops:  26
compiled vars:  !0 = $tests1, !1 = $test, !2 = $tests2, !3 = $test_data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   ASSIGN                                                   !0, <array>
   53     1      > FE_RESET_R                                       $5      !0, ->10
          2    > > FE_FETCH_R                                               $5, !1, ->10
   54     3    >   INIT_FCALL                                               'var_dump'
          4        INIT_FCALL                                               'array_rand_secure'
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $6      
          7        SEND_VAR                                                 $6
          8        DO_ICALL                                                 
   53     9      > JMP                                                      ->2
         10    >   FE_FREE                                                  $5
   57    11        ASSIGN                                                   !2, <array>
   68    12      > FE_RESET_R                                       $9      !2, ->24
         13    > > FE_FETCH_R                                               $9, !3, ->24
   69    14    >   INIT_FCALL                                               'var_dump'
         15        INIT_FCALL                                               'array_rand_secure'
         16        FETCH_DIM_R                                      ~10     !3, 'array'
         17        SEND_VAL                                                 ~10
         18        FETCH_DIM_R                                      ~11     !3, 'num'
         19        SEND_VAL                                                 ~11
         20        DO_FCALL                                      0  $12     
         21        SEND_VAR                                                 $12
         22        DO_ICALL                                                 
   68    23      > JMP                                                      ->13
         24    >   FE_FREE                                                  $9
   70    25      > RETURN                                                   1

Function array_rand_secure:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 47) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
2 jumps found. (Code = 47) Position 1 = 31, Position 2 = 34
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 40
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 42
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 76
Branch analysis from position: 73
1 jumps found. (Code = 42) Position 1 = 77
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 76
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 56
Branch analysis from position: 54
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 42
Branch analysis from position: 59
Branch analysis from position: 42
Branch analysis from position: 56
Branch analysis from position: 34
Branch analysis from position: 30
filename:       /in/HGnAk
function name:  array_rand_secure
number of ops:  79
compiled vars:  !0 = $array, !1 = $num, !2 = $arrayLength, !3 = $randomIndexes, !4 = $randomInt, !5 = $arrayKeys, !6 = $randomKeys
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      1
    5     2        TYPE_CHECK                                  128  ~7      !0
          3        BOOL_NOT                                         ~8      ~7
          4      > JMPZ                                                     ~8, ->13
    6     5    >   INIT_FCALL                                               'trigger_error'
    7     6        GET_TYPE                                         ~9      !0
          7        CONCAT                                           ~10     'array_rand_secure%28%29%3A+expects+parameter+1+to+be+array%2C+', ~9
          8        CONCAT                                           ~11     ~10, '+given'
          9        SEND_VAL                                                 ~11
    8    10        SEND_VAL                                                 512
    6    11        DO_ICALL                                                 
   10    12      > RETURN                                                   null
   12    13    >   COUNT                                            ~13     !0
         14        ASSIGN                                                   !2, ~13
   13    15        IS_IDENTICAL                                             !2, 0
         16      > JMPZ                                                     ~15, ->22
   14    17    >   INIT_FCALL                                               'trigger_error'
         18        SEND_VAL                                                 'array_rand_secure%28%29%3A+Array+is+empty'
         19        SEND_VAL                                                 512
         20        DO_ICALL                                                 
   15    21      > RETURN                                                   null
   18    22    >   INIT_FCALL                                               'filter_var'
         23        SEND_VAR                                                 !1
         24        SEND_VAL                                                 257
         25        DO_ICALL                                         $17     
         26        TYPE_CHECK                                    4  ~18     $17
         27      > JMPNZ_EX                                         ~18     ~18, ->30
   19    28    >   IS_SMALLER_OR_EQUAL                              ~19     !1, 0
         29        BOOL                                             ~18     ~19
         30    > > JMPNZ_EX                                         ~18     ~18, ->34
   20    31    >   SUB                                              ~20     !2, !1
         32        IS_SMALLER                                       ~21     ~20, 0
         33        BOOL                                             ~18     ~21
         34    > > JMPZ                                                     ~18, ->40
   22    35    >   INIT_FCALL                                               'trigger_error'
   23    36        SEND_VAL                                                 'array_rand_secure%28%29%3A+Second+argument+has+to+be+between+1+and+the+number+of+elements+in+the+array'
   24    37        SEND_VAL                                                 512
   22    38        DO_ICALL                                                 
   25    39      > RETURN                                                   null
   28    40    >   ASSIGN                                                   !3, <array>
   29    41      > JMP                                                      ->56
   30    42    >   INIT_FCALL                                               'random_int'
         43        SEND_VAL                                                 0
         44        SUB                                              ~24     !2, 1
         45        SEND_VAL                                                 ~24
         46        DO_ICALL                                         $25     
         47        ASSIGN                                                   !4, $25
   31    48        INIT_FCALL                                               'in_array'
         49        SEND_VAR                                                 !4
         50        SEND_VAR                                                 !3
         51        DO_ICALL                                         $27     
         52        BOOL_NOT                                         ~28     $27
         53      > JMPZ                                                     ~28, ->56
         54    >   ASSIGN_DIM                                               !3
         55        OP_DATA                                                  !4
   29    56    >   COUNT                                            ~30     !3
         57        IS_SMALLER                                               ~30, !1
         58      > JMPNZ                                                    ~31, ->42
   34    59    >   INIT_FCALL                                               'array_keys'
         60        SEND_VAR                                                 !0
         61        DO_ICALL                                         $32     
         62        ASSIGN                                                   !5, $32
   35    63        INIT_FCALL                                               'array_map'
         64        DECLARE_LAMBDA_FUNCTION                          ~34     [0]
         65        BIND_LEXICAL                                             ~34, !5
   37    66        SEND_VAL                                                 ~34
         67        SEND_VAR                                                 !3
   35    68        DO_ICALL                                         $35     
         69        ASSIGN                                                   !6, $35
   39    70        COUNT                                            ~37     !6
         71        IS_IDENTICAL                                             ~37, 1
         72      > JMPZ                                                     ~38, ->76
         73    >   FETCH_DIM_R                                      ~39     !6, 0
         74        QM_ASSIGN                                        ~40     ~39
         75      > JMP                                                      ->77
         76    >   QM_ASSIGN                                        ~40     !6
         77    > > RETURN                                                   ~40
   40    78*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HGnAk
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $key, !1 = $arrayKeys
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   36     2        FETCH_DIM_R                                      ~2      !1, !0
          3      > RETURN                                                   ~2
   37     4*     > RETURN                                                   null

End of Dynamic Function 0

End of function array_rand_secure

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.31 ms | 1022 KiB | 22 Q