The document will be available in your library until the end of your session. To save permanently, please create an account. This mainly cause some problem with people using foreach. The correct way to handle this is explained by that example:.
// You can then correctly use the foreach, as it require an array to work. Print_r($rand_keys); // verify here the array of keys. Anonymous ¶7 years ago If the array elements are unique, and are all integers or strings, here is a simple way to pick $n random *values* (not keys) from an array $array:. // using the above function to pick random values instead of entries. When num_req = 1, the array_rand() function returns an integer that signifies a randomly picked key of the original array. 1 elements: 2.0619630813599E-05 sec. for array_rand,8.4352493286133E-05 sec. for fake_array_rand.
?> [email protected] DOT com ¶13 years ago It is correct that using array_rand() with num_req=1 will return an integer and not an array, but why get so complicated with getting just the one value. While the internal array_rand is faster for a small number of elements, it scales very poorly. ?> josh at 3io dot com ¶14 years ago I modified fake_array_rand to always only return 1 element, and did some benchmarks against calling array_rand with the second parameter as 1. I ran 100 samples for each function for each number of elements and took the average result. Hope this clarifies things … it works for me. // If you use foreach with one element, it won’t work.