我正在从5-10生成一个整数。
一旦生成了一个从5到10的整数,循环应该根据生成的随机整数在数组中选择一个具有character length
的字符串。
示例:
随机整数生成:10
系统应该显示一个长度为10 character
的字符串,所以要么显示testNames1
,要么显示testNames2
,因为它有10 character length
。
我试过在我的代码上这样做,但它不起作用。
<?php
function getName(){
$names = array(
'Juans',
'Luisss',
'Pedroaa',
'testNames1',
'testNames2',
'testName3',
'test', //This should not be return because im only generating a number random of 5-10 and this character has only 4 character length
'tse', //This should not be return because im only generating a number random of 5-10 and this character has only 3 character length
// and so on
);
$randomString = "";
for ($i = 0; $i <count($names); $i++) {
$value = rand(5,10);
$length_string = strlen($names[$i]);
if ($value == $length_string) {
return "name:".$names[$i].$value;
}
}
}
echo getName();
?>
有人能帮我解决这个问题吗?
发布于 2021-06-07 06:34:20
您需要做的是首先创建一个与长度匹配的字符串列表,然后从它们中随机选择一个字符串。所以循环遍历所有的字符串,检查长度并添加到$match
,然后使用array_rand()
选择一个匹配的字符串.
function getName($random_num)
{
$names = array
(
'Juans',
'Luisss',
'Pedroaa',
'testNames1',
'testNames2',
'testName3',
'test', //This should not be return because im only generating a number random of 5-10 and this character has only 4 character length
'tse', //This should not be return because im only generating a number random of 5-10 and this character has only 3 character length
// and so on
);
$randomString = "";
$match = [];
foreach ( $names as $name )
{
$length_string = strlen($name);
if ($random_num == $length_string)
{
$match[] = $name;
}
}
// If non found return ''
return !empty($match) ? $match[array_rand($match)] : '';
}
发布于 2021-06-07 06:19:22
您可以尝试下面的代码。您在循环中添加了下面的行,这是错误的。
$value = rand(5,10);
现在,这个程序将返回与生成的随机数相等的名称,即在5到10之间。如果根据随机数不存在名称,则返回消息。例如:8
<?php
function getName($random_num)
{
$names = array
(
'Juans',
'Luisss',
'Pedroaa',
'testNames1',
'testNames2',
'testName3',
'test', //This should not be return because im only generating a number random of 5-10 and this character has only 4 character length
'tse', //This should not be return because im only generating a number random of 5-10 and this character has only 3 character length
// and so on
);
$randomString = "";
for ($i = 0; $i <count($names); $i++)
{
$length_string = strlen($names[$i]);
if ($random_num == $length_string)
{
return "name:".$names[$i].$random_num;
}
}
}
$random_num = rand(5,10);
$returned_name = getName($random_num);
if ($returned_name == "")
{
echo "No Name Exist having length: " . $random_num;
}
else
{
echo $returned_name;
}
?>
发布于 2021-06-07 08:02:24
你快到了..。您只需在外面生成随机数,然后进行如下检查:
<?php
function getName($random_num)
{
$matches= [];
$names = array(
'Juans',
'Luisss',
'Pedroaa',
'testNames1',
'testNames2',
'testName3',
'test', //This should not be return because im only generating a number random of 5-10 and this character has only 4 character length
'tse', //This should not be return because im only generating a number random of 5-10 and this character has only 3 character length
// and so on
);
$value = rand(5,10);
for ($i = 0; $i <count($names); $i++) {
if(strlen($names[$i]) == $value)
{
$matches[] = $names[$i];
}
}
print_r($matches);
}
?>
https://stackoverflow.com/questions/67866338
复制相似问题