ttmt_73 Posted June 14, 2013 Share Posted June 14, 2013 Hi all I have an array of phrases. I'd like to randomly pick phrases from the array in a loop. I don't want to pick the same phrase more then once in the loop. I thought I could randomly pick the phrase and then delete it before the next loop. http://codepad.org/11l0nStX <?php for($i=0; $i<16; $i++){ $phrases = array('Hello Sailor','Acid Test','Bear Garden','Botch A Job','Dark Horse', 'In The Red','Man Up','Pan Out','Quid Pro Quo','Rub It In','Turncoat', 'Yes Man','All Wet','Bag Lady','Bean Feast','Big Wig'); $ran_Num = array_rand($phrases); $ran_Phrase = $phrases[$ran_Num]; unset($phrases[$ran_Phrase]); echo $ran_Phrase."\r\n"; echo count($phrases)."\r\n"; } ?> Is it possible to randomly pick a different phrase from the array on each loop. Quote Link to comment Share on other sites More sharing options...
louie35 Posted June 14, 2013 Share Posted June 14, 2013 unset($phrases[$ran_Num]); Quote Link to comment Share on other sites More sharing options...
kicken Posted June 14, 2013 Share Posted June 14, 2013 It's generally easier to just randomize the array and then start take what you want off the top. Since you seem to be using all the array entries, just foreach the randomized array. $phrases = array('Hello Sailor','Acid Test','Bear Garden','Botch A Job','Dark Horse', 'In The Red','Man Up','Pan Out','Quid Pro Quo','Rub It In','Turncoat', 'Yes Man','All Wet','Bag Lady','Bean Feast','Big Wig'); shuffle($phrases); foreach ($phrases as $p){ echo $p, '<br>'; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.