Arrays
|
|
|
|
<?php function fake_array_rand ($array, $num_req = 1)
{
# Ensure that $num_req has a sane value
$num_req > 0 or $num_req = 1;
$count = count ($array);
for ($n=0; $n < $num_req; ++$n)
{
# Help keep the number generator random :)
$randval
and usleep ("0.$randval");
# Seed the random number generator
# Generate a random number
srand ((double) microtime() * 10000000);
$randval = rand();
# Use the random value to 'pick' an entry from the array
# Count the number of times that the entry is picked
++$index[$randval % $count];
}
# Loop through the array
for ($n = 0; $n < $count; ++$n, next ($array))
{
# When we get to an entry that has been picked
# Put the entry into an array for output
# Don't forget to decrease the counter
while ($index[$n])
{
$out[] = key ($array);
--$index[$n];
}
}
# Return a string if only on random key was asked for
# otherwise return an array
return ($num_req == 1) ? $out[0] : $out ;
}
/*
DESCRIPTION
============================================================
fake_array_rand:
Duplicates the functionality of PHP 4's array_rand function
in PHP 3. Handy for people who have not yet been able to
convince their sysadmins to upgrade to PHP 4, or for those
who need a starting point to create a function similar to
array_rand.
============================================================
CREDITS
============================================================
Author: J. A. Greant ( zak@nucleus.com )
Version 1: September 1, 2000
============================================================
NOTICES
============================================================
If you:
Modify this code and want to share your changes :)
Find a bug
Have other questions or comments
Please write:
Zak Greant (zak@nucleus.com)
============================================================
USAGE EXAMPLES
============================================================
# pick a random entry from an array
print $array[ fake_array_rand($array) ];
# grab 3 random array keys
$rand_keys = fake_array_rand ($array, 3);
============================================================
COPYRIGHT
============================================================
Copyright (c) 2000 J. A. Greant ( zak@nucleus.com )
All rights reserved.
This function is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General
Public License as published by the Free Software Foundation;
either version 2.1 of the License, or (at your option) any
later version.
This function is distributed in the hope that it will be
useful,but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General
Public License along with this function; if not, write to
the:
Free Software Foundation, Inc.
59 Temple Place, Suite 330
Boston, MA
02111-1307 USA
http://www.gnu.org/
============================================================
*/
?>
|
|
|
Usage Example
|
# pick a random entry from an array
print $array[ fake_array_rand($array) ];
# grab 3 random array keys
$rand_keys = fake_array_rand ($array, 3);
|
|
|
Rate This Script
|
|
|
|