php4-beta | 200004
Date: 04/29/00
- Next message: Sascha Schumann: "[PHP4BETA] cvs: /php4 php_virtual_cwd.c php_virtual_cwd.h"
- Previous message: Cynic: "Re: [PHP4BETA] array_rand() feedback"
- Maybe in reply to: Andrei Zmievski: "[PHP4BETA] array_rand() feedback"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Addressed to: Andrei Zmievski <andrei <email protected>>
php4beta <email protected>
** Reply to note from Andrei Zmievski <andrei <email protected>> Sat, 29 Apr 2000 16:00:14 -0500
>
> 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?
It would sometimes be useful, but not always. Maybe make it another
function?
> 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.
>
$Entries = hash_rand( $var, $count );
while( list( $key, $val ) = each( $Entries )) {
echo "The random choices are: $key - $val<BR>\n";
}
I would probably want it to make sure that there are no duplicates in
the output array. It would be an error if $count was > count( $var ).
If each() only evaluates its argument once when called in a while like
this, then the $Entries variable is not needed.
Rick Widmer
http://www.developersdesk.com
-- 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: Sascha Schumann: "[PHP4BETA] cvs: /php4 php_virtual_cwd.c php_virtual_cwd.h"
- Previous message: Cynic: "Re: [PHP4BETA] array_rand() feedback"
- Maybe in reply to: Andrei Zmievski: "[PHP4BETA] array_rand() feedback"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

