array_rand() feedback
| From: | Andrei Zmievski | Date: | Sat, 29 Apr 2000 21:00:14 +0000 |
| Subject: | array_rand() feedback | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-14179@lists.php.net to get a copy of this message | ||
I wrote a function called array_rand() that is supposed to return a
random element from the input array. After I wrote it, I asked myself:
would it be useful to return the key for that value as well as the value
itself? What do you think?
Example (without key):
$ar = array(1, 2, 3);
$val = array_rand($ar);
Example (with key):
$ar = array("one"=>1, "two"=>2, "three"=>3);
list($key, $val) = array_rand($ar);
Let me know what you think. I also thought about adding a second
argument that specifies how many random elements to return. If that gets
added then returning keys and values for multiple arguments gets a
little trickier.
-Andrei
* Unix is user friendly, it is just chooses its users selectively. *