2017-10-16 4 views
-3

J'ai besoin d'écho une série de quatre chaînes en fonction d'un résultat précédent d'un array_rand(). J'ai cela, jusqu'à présent:PHP: Echo valeur aléatoire de tableau en fonction d'une autre valeur précédente, également aléatoire

<?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>"; 

?> 

J'ai besoin de la sortie pour être quelque chose comme:

"Titre 2

word2, word4, word5, word7", où ces mots sont en réalité liés à le titre principal.

(mots varient d'un groupe à l'autre. Je viens d'utiliser les mêmes noms pour le rendre plus facile à comprendre.)

Il devrait faire écho à différentes valeurs chaque fois que j'actualisez la page.

// Modifier pour plus d'explications //

Quand rafraîchir, il doit faire écho un autre titre et une autre série de 4 mots liés à ce titre.

P.S .: Srsly, pourquoi les votes vers le bas? Au moins l'expliquer.

+2

Stockez vos données différemment et le problème se résout presque : '$ data = ['title1' => ['word1', ...], ...]'. – deceze

+1

Si vous avez des noms de variable "numérotés", alors vous pouvez généralement vous rendre la vie plus facile en utilisant un tableau décent ... reconstruire votre tableau de mots de titre comme '$ title_words = array ( 'title1' => array ('word1', ' word2 ',' word3 ',' word4 ',' word5 ',' word6 ',' word7 '), ' title2 '=> tableau (' mot1 ',' mot2 ',' mot3 ',' mot4 ',' mot5 ',' word6 ',' word7 '), ' title3 '=> (' mot1 ',' mot2 ',' mot3 ',' mot4 ',' mot5 ',' mot6 ',' mot7 '), ' title4 '=> array (' word1 ',' word2 ',' word3 ',' word4 ',' word5 ',' word6 ',' word7 '), ); ' –

+0

Et le problème est ...? Je viens de copier-coller votre code sur mon PHP, ça marche. Les puristes suggéreront des modifications (comme un tableau multi-dimensionnel), mais cela produira ce que vous voulez. – Nic3500

Répondre

1

Comme expliqué dans les commentaires, la meilleure façon de traiter cette question serait de stocker vos mots dans un tableau à deux dimensions, en tant que tel:

$words = array(
    'title1' => array('word1','word2','word3','word4','word5','word6','word7')), 
    'title2' => array('word1','word2','word3','word4','word5','word6','word7')), 
    'title3' => array('word1','word2','word3','word4','word5','word6','word7')), 
    'title4' => array('word1','word2','word3','word4','word5','word6','word7')), 
); 

Et puis utilisez $words[$titles[$rand_title]] comme vos mots sous-tableau.

Cependant, si vous ne pouvez pas (ou ne veulent absolument pas) utiliser des tableaux en deux dimensions, vous pouvez toujours utiliser variable variables:

<?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>"; 

$title = $titles[$rand_title]; // should be title2, for instance 
$words_array = ${$title . '_words'}; // should be $title2_words, for instance 

$rand_words = array_rand($words_array, 4); 
echo $words_array[$rand_words[0]] . "<br>"; 
echo $words_array[$rand_words[1]] . "<br>"; 
echo $words_array[$rand_words[2]] . "<br>"; 
echo $words_array[$rand_words[3]] . "<br>"; 

?> 
+0

C'est tout. Merci beaucoup @ roberto06. Le seul changement nécessaire - mais c'était sur moi - remplaçait echo $ rand_title par echo $ titles [$ rand_title] –