我正在做一个小功能来生成随机用户名,如下所示:
public static function nicknames($data) {
if ($data['request'] == 'nickAvailable') {
foreach ($data as $value)
if (is_array($value))
$nick = $value['nickname'];
$random = rand(2, 2);
$nickname = $nick . '_' . $random;
$count = 3;
$nicknames = array();
for ($i = 1; $i <= $count; $i++) {
$select = self::$db->select('users', 'nickname', array('nickname' => $nickname));
if (count($select) == 0) {
$nicknames[] = $nickname;
} else {
$count = $count + 1;
}
}
$array = array("status" => 0,
"errors" => $nicknames,
"data" => array());
model::json($array);
}
}
我唯一的问题$random
是只执行一次,而不是在每个循环上执行。我需要 3 个不同的用户名在数组中,并且它们必须彼此不同。如何编辑我的代码来实现这一点?感谢您的任何建议