PHP Programing language

adplus-dvertising
array_rand() Function in PHP
Previous Home Next
adplus-dvertising

array_rand()

The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.

Syntax:
array_rand(array,number)

In above syntax, "array" specifies an array and "number" specifies how many random keys to return

Example:

<?php
$a=array("Apple","Banana","Orange","Mango","Guava");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]."<br>";
echo $a[$random_keys[1]]."<br>";
echo $a[$random_keys[2]];
?>

Output:

Apple
Banana
Guava

Previous Home Next