我想生成6位数的随机数并检查它是否在表中,如果它存在则生成另一个6位数的随机数并再次检查等等......代码wriiten正在跟随,
$querySel1=mysqli_query($connect, "select dynamic_email_id from logis_contacts");
while($row = mysqli_fetch_row($querySel1)){
$dynamic_id[] = $row[0];
}
$number = mt_rand( 100000, 999999);
dynamicEmailId($number, $dynamic_id);
function dynamicEmailId($number, $dynamic_id){
if( in_array($number, $dynamic_id) ){
$new = mt_rand( 100000, 999999);
dynamicEmailId($new, $dynamic_id);// here i am confused
}
else {
echo $number;
}
}
请建议更好的方法来实现这个目标
答案 0 :(得分:1)
*没关系,请检查
function generateUniqueId($number,$fetchArray) {
if (!in_array($number,$fetchArray)) {
return $number;
}
else {
$newNumber = mt_rand( 100000, 999999);
return generateUniqueId($newNumber, $fetchArray);
}
}