我想生成唯一的字符串。
我的代码是:
function string(){
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str= '';
for ($i = 0; $i < 3; $i++) {
$str.= $characters[rand(0, strlen($characters) - 1)];
}
return $str;
}
以上代码通过I&#39; m生成唯一字符串,但该字符串不得位于数组下面
$array = array('adc','Fs5','sf9','9Sf', ..........);
任何人都知道怎么做?
答案 0 :(得分:2)
$array = array('adc','Fs5','sf9','9Sf', ..........);
function string() {
global $array;
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
do {
$str= '';
for ($i = 0; $i < 3; $i++) {
$str.= $characters[rand(0, strlen($characters) - 1)];
}
} while (in_array($str, $array));
return $str;
}