3v4l.org

run code in 300+ PHP versions simultaneously
<?php function random_index(array $source) { $max = count($source) - 1; $r = random_int(0, $max); $k = array_keys($source); return $k[$r]; } $array = [ 'apple' => 1234, 'boy' => 2345, 'cat' => 3456, 'dog' => 4567, 'echo' => 5678, 'fortune' => 6789 ]; $i = random_index($array); var_dump([$i, $array[$i]]);

preferences:
46.89 ms | 406 KiB | 5 Q