for a cryptographically secure version, try
<?php
function array_rand_cryptographically_secure(array $array){
$max = count ( $array ) - 1;
if ($max < 0) {
throw new ValueError ( 'Argument #1 ($array) cannot be empty' );
}
return key ( array_slice ( $array, random_int ( 0, $max ), 1, true ) );
}
$tests = [
[5, 6, 7],
['a' => 1, 'b' => 2, 'c' => 3],
['zero', 4 => 'four', 9 => 'nine'],
["PEAN"=>0],
[]
];
foreach ($tests as $test) {
echo array_rand_cryptographically_secure($test) . "\n";
}
?>
(this is an improved version, which unlike the first version, avoids copying *all* the keys)