note 90644 deleted from function.array-rand by danbrown

From: Date: Mon, 14 Sep 2009 16:58:17 +0000
Subject: note 90644 deleted from function.array-rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160554@lists.php.net to get a copy of this message
Note Submitter: webmaster at askapache dot com ---- Quick Tips: to use array_rand to display/return a random item from an array: <?php // Using array_rand $a=array( 'one', 'two', 'three' ); echo $a[array_rand($a)]; // 'one' or 'two' or 'three' // On associative arrays $b=array( 'G'=>'Good', 'B'=>'BAD', 'O'=>'OK' ); echo array_rand($b); // 'G' or 'B' or 'O' echo $b[array_rand($b)]; // 'Good' or 'BAD' or 'OK' // Much nicer than these other methods that aren't pretty $rand=rand(0,sizeof($a)); echo $a[$rand]; shuffle($a); echo $a[0]; shuffle($b); $r=array_values($b); echo $r[rand(0,sizeof(array_keys($b)))]; ?>

« previous php.notes (#160554) next »