Jump to content

Recommended Posts

Hey guys, I was wondering, how would I go about it so I can have a multi-dimensional array where the first value is a string, 2nd value is a minimum amount, 3rd value is a maximum amount.
And then once having that being able to randomly select one of the strings (like using $a = rand(0,14); $test =$name[$a])
but in conjunction being able to generate that string a random amount of times within the min and max without hvaing to use switch statements or if statements?
If that is possible that is xD
And if it is, I know it's not the most efficent way of doing this, it's just a project ive been given to do in this way
Many thanks for any help
James :3

Link to comment
https://forums.phpfreaks.com/topic/280893-multidimensional-array/
Share on other sites

Seems like it might be homework, but oh well:

$array = array(array('string1', 2, 10),
               array('string2', 5, 11),
               array('string3', 9, 25)
);
          
$rand = mt_rand(0, count($array)-1);
$word = $array[$rand][0];
$result = str_repeat($word, mt_rand($array[$rand][1], $array[$rand][2]));

echo $result;

 

Nah, it's not homework, it's just little challenges im being set in an apprenticeship im in, basically find ways on how to do it to learn how to code in PHP, but thanks, works like a charm ^^

 

Well that makes me feel better  :sweat:   Make sure you learn something.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.