Generates a random number between 1-10 and does not repeat.
Method One: Use the shuffle function.
<?php$arr=range(1,10);shuffle($arr);foreach($arras $values){ echo$values." ";}?> |
Method Two: Use the Array_unique function.
<?php$arr=array();while(count($arr)<10){ $arr[]=rand(1,10); $arr=array_unique($arr);}echoimplode(" ",$arr);?> |
Method Three: With the Array_flip function, the principle is the same, is to remove the duplicate value.
<?php$arr=array();$count1=0;$count= 0;$return= array();while($count< 10) { $return[] = mt_rand(1, 10); $return = array_flip(array_flip($return)); $count= count($return); }foreach($return as$value) { echo$value." "; }echo"<br/>";$arr=array_values($return);// 获得数组的值 foreach($arras$key)echo$key." ";?> |
The above has a reference to Daniel, thank them for their selfless contribution source.
PHP generates random numbers