首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在数组中获得特定长度的字符串?

如何在数组中获得特定长度的字符串?
EN

Stack Overflow用户
提问于 2021-06-07 05:29:30
回答 3查看 140关注 0票数 1

我正在从5-10生成一个整数。

一旦生成了一个从5到10的整数,循环应该根据生成的随机整数在数组中选择一个具有character length的字符串。

示例:

随机整数生成:10

系统应该显示一个长度为10 character的字符串,所以要么显示testNames1,要么显示testNames2,因为它有10 character length

我试过在我的代码上这样做,但它不起作用。

代码语言:javascript
运行
复制
<?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();
?>

有人能帮我解决这个问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-06-07 06:34:20

您需要做的是首先创建一个与长度匹配的字符串列表,然后从它们中随机选择一个字符串。所以循环遍历所有的字符串,检查长度并添加到$match,然后使用array_rand()选择一个匹配的字符串.

代码语言:javascript
运行
复制
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)] : '';
}
票数 1
EN

Stack Overflow用户

发布于 2021-06-07 06:19:22

您可以尝试下面的代码。您在循环中添加了下面的行,这是错误的。

代码语言:javascript
运行
复制
$value = rand(5,10);

现在,这个程序将返回与生成的随机数相等的名称,即在5到10之间。如果根据随机数不存在名称,则返回消息。例如:8

代码语言:javascript
运行
复制
<?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;
}
?>
票数 1
EN

Stack Overflow用户

发布于 2021-06-07 08:02:24

你快到了..。您只需在外面生成随机数,然后进行如下检查:

代码语言:javascript
运行
复制
<?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);
}

?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67866338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档