3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_pick(array $array, bool $weighted = false) { $weight = function($value, int $weight = 1){ $item = new class($value, $weight){ public function __construct($value, int $weight){ $this->value = $value; $this->weight = $weight; } }; return $item; }; foreach($array as $key => $value){ if($weighted){ $items[] = $weight($key, $value); } else { $items[] = $weight($value); } } $max = array_reduce($items, function($c, $i){ $c += $i->weight; return $c; }); $rand = rand(1, $max); foreach($items as $item) { $rand -= $item->weight; if ($rand <= 0) { return $item->value; } } } $array = str_split('qwertyuiopasdfghjklzxcvbnm'); var_dump(array_pick($array));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qg7ie
function name:  (null)
number of ops:  11
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   INIT_FCALL                                               'str_split'
          1        SEND_VAL                                                 'qwertyuiopasdfghjklzxcvbnm'
          2        DO_ICALL                                         $1      
          3        ASSIGN                                                   !0, $1
   41     4        INIT_FCALL                                               'var_dump'
          5        INIT_FCALL                                               'array_pick'
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0  $3      
          8        SEND_VAR                                                 $3
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Function array_pick:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 21
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 21
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 34, Position 2 = 43
Branch analysis from position: 34
2 jumps found. (Code = 78) Position 1 = 35, Position 2 = 43
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 42
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
Branch analysis from position: 21
filename:       /in/qg7ie
function name:  array_pick
number of ops:  45
compiled vars:  !0 = $array, !1 = $weighted, !2 = $weight, !3 = $value, !4 = $key, !5 = $items, !6 = $max, !7 = $rand, !8 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
    5     2        DECLARE_LAMBDA_FUNCTION                          ~9      [0]
          3        ASSIGN                                                   !2, ~9
   15     4      > FE_RESET_R                                       $11     !0, ->21
          5    > > FE_FETCH_R                                       ~12     $11, !3, ->21
          6    >   ASSIGN                                                   !4, ~12
   16     7      > JMPZ                                                     !1, ->15
   17     8    >   INIT_DYNAMIC_CALL                                        !2
          9        SEND_VAR_EX                                              !4
         10        SEND_VAR_EX                                              !3
         11        DO_FCALL                                      0  $15     
         12        ASSIGN_DIM                                               !5
         13        OP_DATA                                                  $15
   16    14      > JMP                                                      ->20
   19    15    >   INIT_DYNAMIC_CALL                                        !2
         16        SEND_VAR_EX                                              !3
         17        DO_FCALL                                      0  $17     
         18        ASSIGN_DIM                                               !5
         19        OP_DATA                                                  $17
   15    20    > > JMP                                                      ->5
         21    >   FE_FREE                                                  $11
   23    22        INIT_FCALL                                               'array_reduce'
         23        SEND_VAR                                                 !5
         24        DECLARE_LAMBDA_FUNCTION                          ~18     [1]
   26    25        SEND_VAL                                                 ~18
   23    26        DO_ICALL                                         $19     
         27        ASSIGN                                                   !6, $19
   28    28        INIT_FCALL                                               'rand'
         29        SEND_VAL                                                 1
         30        SEND_VAR                                                 !6
         31        DO_ICALL                                         $21     
         32        ASSIGN                                                   !7, $21
   30    33      > FE_RESET_R                                       $23     !5, ->43
         34    > > FE_FETCH_R                                               $23, !8, ->43
   31    35    >   FETCH_OBJ_R                                      ~24     !8, 'weight'
         36        ASSIGN_OP                                     2          !7, ~24
   32    37        IS_SMALLER_OR_EQUAL                                      !7, 0
         38      > JMPZ                                                     ~26, ->42
   33    39    >   FETCH_OBJ_R                                      ~27     !8, 'value'
         40        FE_FREE                                                  $23
         41      > RETURN                                                   ~27
   30    42    > > JMP                                                      ->34
         43    >   FE_FREE                                                  $23
   36    44      > 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/qg7ie
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $value, !1 = $weight, !2 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      1
    6     2        DECLARE_ANON_CLASS                               <unknown> 
          3        NEW                                              $4      $3
          4        SEND_VAR_EX                                              !0
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !2, $4
   12     8      > RETURN                                                   !2
   13     9*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qg7ie
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $c, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   24     2        FETCH_OBJ_R                                      ~2      !1, 'weight'
          3        ASSIGN_OP                                     1          !0, ~2
   25     4      > RETURN                                                   !0
   26     5*     > RETURN                                                   null

End of Dynamic Function 1

End of function array_pick

Class class@anonymous:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qg7ie
function name:  __construct
number of ops:  7
compiled vars:  !0 = $value, !1 = $weight
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        ASSIGN_OBJ                                               'value'
          3        OP_DATA                                                  !0
    9     4        ASSIGN_OBJ                                               'weight'
          5        OP_DATA                                                  !1
   10     6      > RETURN                                                   null

End of function __construct

End of class class@anonymous.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.42 ms | 1474 KiB | 18 Q