Easy Tutorial
❮ Func Math Mt Rand Pdostatement Rowcount ❯

PHP array_rand() Function

Complete PHP Array Reference Manual

Example

Returns an array containing random key names:

<?php
$a = array("red", "green", "blue", "yellow", "brown");
$random_keys = array_rand($a, 3);
echo $a[$random_keys[0]] . "<br>";
echo $a[$random_keys[1]] . "<br>";
echo $a[$random_keys[2]];
?>

Definition and Usage

The array_rand() function returns a random key name from the array, or an array of random key names if more than one key name is specified.


Syntax

Parameter Description
array Required. Specifies the array.
number Optional. Specifies how many random elements to return.

Technical Details

Return Value: Returns a random key name from the array, or an array of random key names if more than one key name is specified.
PHP Version: 4+
--- ---
Changelog: As of PHP 5.2.10, the result array of key names is no longer shuffled. <br> <br>As of PHP 4.2.0, the random number generator is automatically seeded.
--- ---

More Examples

Example 1

Returns a random key name from the array:

Example 2

Returns an array containing random string key names:

❮ Func Math Mt Rand Pdostatement Rowcount ❯