如何从数组中获取随机值?

问题描述:

我有一个名为 $ran = array(1,2,3,4);

我需要从这个数组中获取一个随机值并将其存储在一个变量中,我该怎么做?

I need to get a random value out of this array and store it in a variable, how can I do this?

你也可以只做:

$k = array_rand($array);
$v = $array[$k];

当你有一个关联数组时,这是这样做的方法.

This is the way to do it when you have an associative array.