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, 4, 5, 6, 7, 8, 9, 10
After: 9, 4, 6, 3, 1, 5, 7, 8, 2, 10
|