-1
我想在数组中生成6个数字,我使用mt_rand函数,但是我不能在数组中使用相同的数字。我将如何去检查这个并重新生成号码?PHP中唯一的数字
我想过做一个重复的数组和循环抛出,并计算数组中有多少,如果它超过1然后重新生成数字并再次检查,但这只是看起来像很多工作的东西该PHP可能有一个本地函数,因为我找不到...
重复的东西不是我需要帮助,即检查值的关键,我需要所有的值是唯一的。
我想在数组中生成6个数字,我使用mt_rand函数,但是我不能在数组中使用相同的数字。我将如何去检查这个并重新生成号码?PHP中唯一的数字
我想过做一个重复的数组和循环抛出,并计算数组中有多少,如果它超过1然后重新生成数字并再次检查,但这只是看起来像很多工作的东西该PHP可能有一个本地函数,因为我找不到...
重复的东西不是我需要帮助,即检查值的关键,我需要所有的值是唯一的。
$number_of_numbers = 6;//number_of_numbers in array
$min = 1;//minimum value of number that you want
$max = 15;//maximum value of number that you want
$array = array();
for ($a = 0; $a < $number_of_numbers; $a++) {
$check = 0;//for entering while loop
while ($check !== false) { //pick a number and check if it is in array if it is in the array, pick again and check again
$try[$a] = mt_rand($min, $max); //pick a number
$check = array_search($try[$a], $array);// search that number if it is in the array
}
$array[] = $try[$a]; //add number to array
}
var_dump($array); // show me the array
这是你的意思吗?我可能会误解数组键。你能解释一下吗?
这里是输出。抱歉,我忘了先添加它。
array(6) { [0]=> int(10) [1]=> int(12) [2]=> int(3) [3]=> int(5) [4]=> int(9) [5]=> int(15) }
的[生成在PHP随机唯一编号的数组]可能的复制(http://stackoverflow.com/questions/10824770/generate-array-of-random-unique-numbers-in-php) – Laurel
这并不是一回事,那就是确保关键和价值不一样。我只需要价值观。 – jnDny
这一个肯定是一个更好的重复:http://stackoverflow.com/questions/7792816/php-unique-random-numbers?rq=1 – Laurel