How to get random value out of an array?

Elitmiar picture Elitmiar · Oct 29, 2009 · Viewed 160.2k times · Source

I have an array called $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?

Answer

reko_t picture reko_t · Oct 29, 2009

You can also do just:

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

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