Log in

View Full Version : [PHP] Question: Selecting Data from Array Randomly



Trigs
22-02-2009, 07:23 PM
If I have an array and I want to select a random value from it would I be able to just do

rand($array);

Or what.

Edit by Robbie! (Forum Moderator) - Moved to Coding Help, please post in the correct section next time, thanks!

Source
22-02-2009, 07:28 PM
This is if you have arrays like $blah['1'];




$arrayC = count( $array );
$arrayC = $arrayC - 1;
$arrayR = rand( 0, $arrayC );

echo $array["$arrayR"];

Should work, sorry if not :P

oorrrrr caleb suggested : http://uk.php.net/array_rand on msn :P didn't know that existed, silly me

Trigs
22-02-2009, 07:38 PM
Exactly what I needed. Thanks.

Want to hide these adverts? Register an account for free!