php4-beta | 200004
Date: 04/29/00
- Next message: Andrei Zmievski: "Re: [PHP4BETA] array_rand() feedback"
- Previous message: Andrei Zmievski: "[PHP4BETA] array_rand() feedback"
- In reply to: Andrei Zmievski: "[PHP4BETA] array_rand() feedback"
- Next in thread: Andrei Zmievski: "Re: [PHP4BETA] array_rand() feedback"
- Reply: Andrei Zmievski: "Re: [PHP4BETA] array_rand() feedback"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Andrei Zmievski wrote:
>
> 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. *
>
I think it would be a good idea to just return keys, not key-value
pairs, since its simple enough to do:
$key = array_rand($ar);
echo $ar[$key]; // value (duh)
I also like the second argument idea, again I think it should be
something like:
list ($key1, $key2, $key3) = array_rand($ar, 3);
Sterling
-- PHP 4.0 Beta Mailing List <http://www.php.net/version4/> To unsubscribe, e-mail: php4beta-unsubscribe <email protected> For additional commands, e-mail: php4beta-help <email protected> To contact the list administrators, e-mail: php4beta-admin <email protected>
- Next message: Andrei Zmievski: "Re: [PHP4BETA] array_rand() feedback"
- Previous message: Andrei Zmievski: "[PHP4BETA] array_rand() feedback"
- In reply to: Andrei Zmievski: "[PHP4BETA] array_rand() feedback"
- Next in thread: Andrei Zmievski: "Re: [PHP4BETA] array_rand() feedback"
- Reply: Andrei Zmievski: "Re: [PHP4BETA] array_rand() feedback"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

