我想生成随机的20.000个名字。 我有3个数组:$ firstNameCollection,$ middleNameCollection和$ lastNameCollection,如下所示:(查看代码),我想生成(随机)20.000个名字。我将附加我的代码,其中只生成1个名称..
$firstNameCollection = array("Harry","Ross",
"Bruce","Cook",
"Carolyn","Morgan",
"Albert","Walker",
"Randy","Reed",
"Larry","Barnes",
"Lois","Wilson",
"Jesse","Campbell",
"Ernest","Rogers",
"Theresa","Patterson",
"Henry","Simmons",
"Michelle","Perry",
"Frank","Butler",
"Shirley");
$middleNameCollection = array("Brooks",
"Rachel","Edwards",
"Christopher","Perez",
"Thomas","Baker",
"Sara","Moore",
"Chris","Bailey",
"Roger","Johnson",
"Marilyn","Thompson",
"Anthony","Evans",
"Julie","Hall",
"Paula","Phillips",
"Annie","Hernandez",
"Dorothy","Murphy",
"Alice","Howard");
$lastNameCollection = array("Ruth","Jackson",
"Debra","Allen",
"Gerald","Harris",
"Raymond","Carter",
"Jacqueline","Torres",
"Joseph","Nelson",
"Carlos","Sanchez",
"Ralph","Clark",
"Jean","Alexander",
"Stephen","Roberts",
"Eric","Long",
"Amanda","Scott",
"Teresa","Diaz",
"Wanda","Thomas");
$newFirstName = $firstNameCollection[rand(0, count($firstNameCollection)-1)];
$newMiddleName = $middleNameCollection[rand(0, count($middleNameCollection)-1)];
$newLastName = $lastNameCollection[rand(0, count($lastNameCollection)-1)];
$fullNameCollection = array();
$fullNameCollection[] = $newFirstName." ".$newMiddleName." ".$newLastName;
如果我用“echo $ fullNameCollection”打印它,它只会给我一个名字......那么..我怎样才能制作20.000个名字?谢谢。
答案 0 :(得分:0)
使用for
循环
$fullNameCollection = array();
for($i = 0; $i < 20000;$i++) {
$newFirstName = $firstNameCollection[rand(0, count($firstNameCollection)-1)];
$newMiddleName = $middleNameCollection[rand(0, count($middleNameCollection)-1)];
$newLastName = $lastNameCollection[rand(0, count($lastNameCollection)-1)];
$fullNameCollection[] = $newFirstName." ".$newMiddleName." ".$newLastName;
}
如果您想输出所有名称,例如
foreach($fullNameCollection as $name) {
echo $name;
}
只有唯一名称
$fullNameCollection = array();
while(count($fullNameCollection) <= 20000) {
$newFirstName = $firstNameCollection[rand(0, count($firstNameCollection)-1)];
$newMiddleName = $middleNameCollection[rand(0, count($middleNameCollection)-1)];
$newLastName = $lastNameCollection[rand(0, count($lastNameCollection)-1)];
$name = $newFirstName." ".$newMiddleName." ".$newLastName;
if(!in_array($name, $fullNameCollection)) {
$fullNameCollection[] = $name;
}
}
注意:如果没有足够的可能性用于唯一名称,则此循环可以是无限的。 如果你想要所有组合,最好不要使用随机函数
答案 1 :(得分:0)
嗯,我猜你的代码周围需要for
循环?
$fullNameCollection = array();
for ($x = 0; $x < 20000; $x++) {
$newFirstName = $firstNameCollection[rand(0, count($firstNameCollection)-1)];
$newMiddleName = $middleNameCollection[rand(0, count($middleNameCollection)-1)];
$newLastName = $lastNameCollection[rand(0, count($lastNameCollection)-1)];
$fullNameCollection[$x] = $newFirstName." ".$newMiddleName." ".$newLastName;
}