Help - Search - Members - Calendar
Full Version: Outputting Random Info From An Array
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
The Seventh Hokage
the best way i have come up with is like so...
CODE
$result=rand(0,9);
$description[0]="hi";
$description[1]="hiya";
/*description filled with random stuff. so on until 10 entries are made*/
switch($result)
{
case 0:
echo $description[0];
break;
//and so on
}


is there a more simple,less code laden way to do this?
Shadow Link
I don't think you need a switch/case for every result. Wouldn't this work?
CODE
echo $description[$result];
Ed
http://php.net/array-rand holds your answer.
NDBoost
QUOTE(Ed @ Aug 31 2009, 02:11 AM) *
http://php.net/array-rand holds your answer.


Yes array-rand is correct.

CODE
<?php
$rand[0] = 'bg';
$rand[1] = 'bg2';
$rand[2] = 'bg3';
$rand[3] = 'bg4';
$rand_keys = array_rand($rand, 4);

echo $rand[$rand_keys[0]];

?>

this is an example from my drupal theme on doing a random value from an array using array_rand();
The Seventh Hokage
thanks. it took me a while to find this post again . i appreciate the help. i need to bookmark php.net too... lol
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.