random element from array - array_rand()

by rajesh 2010-01-02 15:23:06


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.

Tagged in:

1351
like
0
dislike
0
mail
flag

You must LOGIN to add comments