shuffle
bool shuffle(array array)
Randomizes the order of elements in an array.
Returns:
TRUE on success; FALSE on failure
Description:
Randomizes the order of elements in an indexed array. Use srand() to seed the randomizer before calling this function. If you pass an associative array to this function, it randomizes the values but destroys the keys.
After shuffling, array is numerically indexed starting from 0.
Version:
PHP 3 since 3.0.8, PHP 4 since 4.0b4
See also:
array_rand() rand() srand()
Example:
Randomize the elements of an array
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
srand(time());
echo "Before: ", implode(", ", $numbers), "\n";
shuffle($numbers);
echo "After: ", implode(", ", $numbers), "\n";
Output: Before: 1, 2, 3, ... |
Get PHP Functions Essential Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.