I'm trying to generate a random array (if I'm using the term right), WITH possible duplicates but not next to each other, and out of a given array.
Ex with the variables: lions tigers bears monkeys elephants
I want to be able to produce one line, but with the possibilities of: "lions and tigers and bears" OR "bears and lions and monkeys and elephants" OR "monkeys and elephants" OR "elephants and lions and elephants and tigers"
So it would need to require a random number of possibilities (no less than two - so "bears and tigers" and any other product with two of the options would be the minimum - but also no more than seven.) And again, I would like the variables to be available to be duplicated, but not next to each other, so something like "monkeys and lions and tigers and monkeys" would be possible.
Since I don't know a lot about PHP, this is just one of the options I've tried:
<?php
$input_array = array("lions", "tigers", "bears", "monkeys", "elephants");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>
But since it only generates a static amount of two elements and doesn't repeat, it doesn't quite produce what I'm trying to do.
Any help would be appreciated, especially if it includes code for putting the word "and" in between each variable, and also prints them (instead of generating the algorithm) - PHP is very new to me and I'm not sure how to do it without messing up.
Aucun commentaire:
Enregistrer un commentaire