kenneth009 Posted September 24, 2009 Share Posted September 24, 2009 Trying to develop a small app for my site. What is the code to generate a random # of lines (1 min - 5 max) and a random phrase for each line sourced from separate arrays (stored in txt files)? Link to comment https://forums.phpfreaks.com/topic/175304-random-phraseline-generator/ Share on other sites More sharing options...
5kyy8lu3 Posted September 24, 2009 Share Posted September 24, 2009 Trying to develop a small app for my site. What is the code to generate a random # of lines (1 min - 5 max) and a random phrase for each line sourced from separate arrays (stored in txt files)? do a multidimensional array, with numbers as the keys. then use $a = rand(0, 10); or whatever you want for your range to get a random number. echo $array[$a]; Link to comment https://forums.phpfreaks.com/topic/175304-random-phraseline-generator/#findComment-923916 Share on other sites More sharing options...
kenneth009 Posted September 25, 2009 Author Share Posted September 25, 2009 Thank you. Understand the multidimensional array part. How do I prevent random duplication with the generated lines of text? Would it involve a separate random loop function? This would generate the random # of lines with <br> tags at the end. The app needs to be able to generate 1 - 5 lines of text. Each line unduplicated. Link to comment https://forums.phpfreaks.com/topic/175304-random-phraseline-generator/#findComment-924590 Share on other sites More sharing options...
Alex Posted September 25, 2009 Share Posted September 25, 2009 <?php $phrases = Array('Phrase A', 'Phrase B', 'Phrase C', 'Phrase D', 'Phrase E', 'Phrase F', 'Phrase G'); $out = Array(); for($i = 0;$i < 5; $i++) { $key = array_rand($phrases); if(!in_array($phrases[$key], $out)) { $out[] = $phrases[$key]; unset($phrases[$key]); } } echo implode('<br />', $out); Link to comment https://forums.phpfreaks.com/topic/175304-random-phraseline-generator/#findComment-924593 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.