PHP array_rand() Function
Complete PHP Array Reference Manual
Example
Return an array containing random keys:
" ; echo $a [ $random_keys [ 1 ] ] . "" ; echo $a [ $random_keys [ 2 ] ] ; ?>
Run Example »
Definition and Usage
The array_rand() function returns a random key from an array, or if you specify that the function should return more than one key, it returns an array of random keys.
Syntax
array_rand(
array,number
)
| Parameters | Description |
|---|---|
| array | Required. Specifies the array. |
| number | Optional. Specifies how many random elements to return. |
Technical Details
| Return Value: | Returns a random key from the array, or if you specify that the function should return more than one key, returns an array containing the random keys. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | Since PHP 5.2.10, the resulting array of keys is no longer shuffled. |
自 PHP 4.2.0 起,随机数生成器会自动播种。 |
More examples
Example 1
Return a random key from an array:
"red","b"=>"green","c"=>"blue","d"=>"yellow"); print_r(array_rand($a,1)); ?>Run Example »
Example 2
Return an array containing random string keys:
"red","b"=>"green","c"=>"blue","d"=>"yellow"); print_r(array_rand($a,2)); ?>Run Example »
Complete PHP Array Reference Manual