Perad Posted November 19, 2007 Share Posted November 19, 2007 I have an array which has $key and $values set. I want to randomly select one of these values. How would I do this? Link to comment https://forums.phpfreaks.com/topic/77940-solved-select-random-array-value/ Share on other sites More sharing options...
ToonMariner Posted November 19, 2007 Share Posted November 19, 2007 $select = rand(0, count($arr) - 1); echo $arr[$select]; Link to comment https://forums.phpfreaks.com/topic/77940-solved-select-random-array-value/#findComment-394505 Share on other sites More sharing options...
Wes1890 Posted November 19, 2007 Share Posted November 19, 2007 Like this: $array_name[rand(0, count($array_name)-1 )]; -- rand(min,max) selects a random number in between min, and max. count(array) counts how many arrays values are in an array Link to comment https://forums.phpfreaks.com/topic/77940-solved-select-random-array-value/#findComment-394506 Share on other sites More sharing options...
Perad Posted November 19, 2007 Author Share Posted November 19, 2007 Wes, in regards to your answer, what are the square brackets for? Link to comment https://forums.phpfreaks.com/topic/77940-solved-select-random-array-value/#findComment-394512 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.