class="php" name="code">/** * 根据当前无重复的$codes,补充生成长度为$length,总数为$amount的 * * @param int $length * 长度 * @param int $amount * 需要总数 * @param string $prefix * 前缀 * @param array $codes * 初始codes * @return array $codes 无重复的codes,含输入参数 */ myrand($length, $amount = 1, $prefix = '', $codes = []) { $seed = [ '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; $codes_tmp = []; $need = $amount - count($codes); for ($c = 0; $c < $need; $c++) { $code = ''; for ($i = 0; $i < $length; $i++) { $index = rand(0, 31); $code .= $seed[$index]; } $codes_tmp[] = $prefix . $code; } $codes = array_unique(array_merge($codes, $codes_tmp)); unset($codes_tmp); $number = count($codes); $d = $amount - $number; if ($d > 0) { $codes = array_unique(array_merge($codes, myrand($length, $amount, $prefix, $codes))); } return $codes; }
?