SQL PHP HTML ASP JavaScript articles and free scripts to download
 

PHP Random collection of element from an array

We can randomly collect one or more element from an array by using array_rand() function in PHP. We can specify the number of random elements required by specifying an optional parameter in side the function. 













$value= array("Rabin","Reid","Cris","KVJ","John");

$rand_keys=array_rand($value,2);
echo "First random element = ".$value[$rand_keys[0]];
echo "<br>Second random element = ".$value[$rand_keys[1]];


We can create a six digit random number generator which will give us only characters.

srand ((double) microtime() * 10000000);
$input = array ("A", "B", "C", "D", "E","F","G","H","I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z");
$rand_index = array_rand($input,6);
print $input[$rand_index[3]];
print $input[$rand_index[5]];
print $input[$rand_index[4]];
print $input[$rand_index[2]];
print $input[$rand_index[1]];
print $input[$rand_index[0]];

By using above code we can generate 6 digit random number of only characters. You can read how to generate random numbers here.
Read here how to generate random password string having alphabets and digits






Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked


Join Our Email List
Email:  
For Email Newsletters you can trust
Array Functions
PHP Sections