PHP array_rand()


array_rand() randomly select one or more elements of an array. If only select one element, the index of the element will be returned, else if the selected elements number is >1, then an array will be returned.

<?PHP
	$arr1=array(1,2,3,4,5,6,7,8,9);
	$arr2=array_rand($arr1,2);
	foreach($arr2 as $element) echo "$element, ";  //0, 7, 
	$ret=array_rand($arr1,1);
	echo "$ret";  //5
	$arr1=array("rice","corn","meat","fish");
	$arr2=array_rand($arr1,2);
	foreach($arr2 as $element) echo "$element, ";  //1, 3,  
?>