lundi 16 octobre 2017

PHP: Echo random value from array depending on another previous value, also random

I need to echo a series of four strings depending on a previous result of a array_rand(). I have this, so far:

<?php

$titles = array('title1','title2','title3','title4');

$title1_words = array('word1','word2','word3','word4','word5','word6','word7');
$title2_words = array('word1','word2','word3','word4','word5','word6','word7');
$title3_words = array('word1','word2','word3','word4','word5','word6','word7');
$title4_words = array('word1','word2','word3','word4','word5','word6','word7');

$rand_title = array_rand($titles, 1);
echo $rand_title . "<br>";


//print 4 random words from one group
$rand_words = array_rand($title1_words, 4);
echo $title1_words[$rand_words[0]] . "<br>";
echo $title1_words[$rand_words[1]] . "<br>";
echo $title1_words[$rand_words[2]] . "<br>";
echo $title1_words[$rand_words[3]] . "<br>";

?>

I need the output to be something like:

"Title 2

word2, word4, word5, word7", where those words are actually related to the main title.

(Words vary from group to group. I just used the same names to make it easier to understand.)

It is expected to echo different values everytime I refresh the page.




Aucun commentaire:

Enregistrer un commentaire