我試圖給我的數據庫播種示例用戶。我希望每個用戶不要相同,但是我的代碼生成了10個相同的用戶。在推送到數組之前,如何構建循環以創建新的隨機用戶?如何在PHP中創建隨機用戶以種子數據庫
$frst = array('Bob','Joe','Kim','Gary','David','Vasili','Fred','Seaton','Steve','Lou','Greg');
$last = array('Jones','Allen','Darling','Foster','Johnson','Hall','Lynch','Wilson','Baldwin','Largent','Shelton','Porter');
$role = array('Architect','Electrical','General','Mechanical');
$rand_frst = array_rand($frst, 4);
$rand_last = array_rand($last, 4);
$rand_role = array_rand($role, 4);
$users = [];
$i = 1;
$user = [
'first_name' => $frst[$rand_frst[0]],
'last_name' => $last[$rand_last[0]],
'email' => $frst[$rand_frst[0]] . '@' . $last[$rand_last[0]] . '.com',
'password' => $last[$rand_last[0]],
'company_id' => rand(1, 5),
'username' => $frst[$rand_frst[0]] . $last[$rand_last[0]],
'zipcode' => rand(98101, 98999),
'role' => $role[$rand_role[0]]
];
while ($i <= 10)
{
array_push($users, $user);
$i++;
}
var_dump($users);
我試圖把$用戶陣列while循環中,但我得到了相同的結果
在循環中做你的隨機化。 –