random element from array - array_rand() - PHP Views : 389
Tagged in : PHP
0 0
Send mail

array_rand — Pick one or more random entries out of an array

This function can be used to fetch one or more elements from an array in php.

Example:

<?php
$a = array(1,2,3,4,5);
$result = array_rand($a);
echo $result;
?>


This will chose a random number from the array $a.

To chose more than one random element from array we have to pass a second argument.
Example:

<?php
$a = array(1,2,3,4,5);
$result = array_rand($a,2);
echo $result;
?>


This returns two random elements.
By rajesh, On - 2010-01-02



    Login to add Comments .