1

I am generating an integer from 5-10.

once I generate an integer ranging from 5-10, the loop should choose a string on my array that has a character length based on the random integer that is generated.

example:

random integer generated: 10

the system should display a string that has a length of 10 character so either it will display the testNames1 or testNames2 because it has 10 character length

I tried doing this on my code, but it doesn't work.

<?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();
?>

Can anyone help me on how to do this?

3 Answers 3

1

What you need to do is first create a list of strings matching the length and then pick a random one from them. So loop through all of the strings, check the length and add to $match, then use array_rand() to pick one of the matching ones...

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)] : '';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. this helps me a lot Sir! I already understand my mistake, this solves too the choosing of one value in multiple value that has same length of character! thank you sir!
1

You can try the below code. You was adding the below line in the loop that was wrong.

$value = rand(5,10);

Now, This program will return the name that will be equal to the generated random number that is between 5 to 10. Also it will return the message if no name exist according to the random number. For example: 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;
}
?>

5 Comments

OH! I WAS SO CLOSE ON MY PROBLEM! :(. THANK YOU VERY MUCH SIR! I APPREICATE IT SO MUCH
sir can i still ask, what if there is 2 string that has a same lenght of character? how can i randomly choose 1 from them?
This program will return only the first one from the array. It will not return 2 values.
is there a way sir to randomly choose one from the 2 values that has same length?
Yes, in that case you have to return the array from the function. and after that use the array_rand function to select one key from the array randomly
1

you are almost there ... you just need to generate random number outside then make checks like this :

<?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);
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.