Php 如何在数组中获取特定长度的字符串?
我正在生成一个5-10的整数 生成5-10之间的整数后,循环应在数组中选择一个字符串,该字符串的字符长度基于生成的随机整数 例如: 生成的随机整数:10 系统应显示一个长度为10个字符的字符串,以便显示testNames1或testNames2,因为它的长度为10个字符 我试着在我的代码上这样做,但不起作用Php 如何在数组中获取特定长度的字符串?,php,arrays,Php,Arrays,我正在生成一个5-10的整数 生成5-10之间的整数后,循环应在数组中选择一个字符串,该字符串的字符长度基于生成的随机整数 例如: 生成的随机整数:10 系统应显示一个长度为10个字符的字符串,以便显示testNames1或testNames2,因为它的长度为10个字符 我试着在我的代码上这样做,但不起作用 <?php function getName(){ $names = array( 'Juans', 'Luisss',
<?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();
?>
有人能帮我做这件事吗?你可以试试下面的代码。您在循环中添加了下面的行,这是错误的
$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;
}
?>
您可以尝试下面的代码。您在循环中添加了下面的行,这是错误的
$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;
}
?>
您需要做的是首先创建一个与长度匹配的字符串列表,然后从中随机选取一个字符串。因此,循环遍历所有字符串,检查长度并添加到$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)] : '';
}
您需要做的是首先创建一个与长度匹配的字符串列表,然后从中随机选取一个字符串。因此,循环遍历所有字符串,检查长度并添加到$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)] : '';
}
你就快到了。。。您只需在外部生成随机数,然后进行如下检查:
<?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);
}
?>
你就快到了。。。您只需在外部生成随机数,然后进行如下检查:
<?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个?此程序将仅返回数组中的第一个。它不会返回2个值。是否有方法从2个长度相同的值中随机选择一个?是的,在这种情况下,您必须从函数返回数组。然后使用array_rand函数从数组中随机选择一个键!我离我的问题太近了!:。非常感谢,先生!我非常感激它,所以我仍然可以问,如果有两个字符串具有相同的字符长度,该怎么办?如何从中随机选择1个?此程序将仅返回数组中的第一个。它不会返回2个值。是否有方法从2个长度相同的值中随机选择一个?是的,在这种情况下,您必须从函数返回数组。然后使用array_rand函数从数组中随机选择一个键谢谢。这对我很有帮助,先生!我已经理解了我的错误,这也解决了在具有相同字符长度的多个值中选择一个值的问题!谢谢你,先生!非常感谢。这对我很有帮助,先生!我已经理解了我的错误,这也解决了在具有相同字符长度的多个值中选择一个值的问题!谢谢你,先生!