Hi,
I want to output a random value from an array. i use the array_rand() function but this function returns the array key while i want echo the value.

$mtn = array(08034, 08035, 08036,08037);
$mtnwinner = array_rand($mtn, $num_req = 1);
echo $mtnwinner;

the above code will return any of the following values 0,1,2,3.

Recommended Answers

All 5 Replies

<?
$mtn = array('08034', '08035', '08036','08037');
$rand_key = array_rand($mtn,1);
echo $mtn[$rand_key];
?>

vibhaj

echo $mtnwinner[0] would only work if i was selecting more than value.

i have edited post.. Have you checked that?

Member Avatar for diafol

Maybe?

$mtn = array(08034, 08035, 08036,08037);
shuffle($mtn);
echo $mtn[0];

many ways:

<?
$mtn = array('08034', '08035', '08036','08037');
echo $mtn[rand(0, (count($mtn)-1))];
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.