note 87051 deleted from function.array-rand by danbrown
| From: | danbrown@php.net | Date: | Fri, 12 Dec 2008 23:03:10 +0000 |
| Subject: | note 87051 deleted from function.array-rand by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148109@lists.php.net to get a copy of this message | ||
Note Submitter: vista_ at live dot se
----
Do you think it is ugly to output $array[array_rand($array)] ? Use this!
<?php
if(!function_exists('array_rand_'))
{
function array_rand_($array, $limit = 1)
{
return $array[array_rand($array, $limit)];
}
}
?>
Before: <?php echo $array[array_rand($array)]; ?>
After: <?php echo array_rand_($array); ?>
If you don't want to create a variable, this is great fun. You can write <?php echo
array_rand_(array(1,2,3)); ?> :)