zipp Posted July 23, 2007 Share Posted July 23, 2007 Hello, Im trying to make a script that randomly generates 4 numbers from an array, without duplicates. I have the random generation done, but i cant figure out how to get rid of duplicates. Here is what I have so far: <?php $quote = array( 1 => "abc", 2 => "def", 3 => "hij", 4 => "klm", 5 => "nop", 6 => "qrs", 7 => "tuv", 8 => "wxyz", ); $i = 1; while ($i <= 4) { $randnum = rand(1,7); echo"$quote[$randnum] <br />"; $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/61414-solved-random-array/ Share on other sites More sharing options...
Barand Posted July 23, 2007 Share Posted July 23, 2007 shuffle ($quote); // shake them up $rand_array = array_slice ($quote, 0, 4); // take the top 4 Link to comment https://forums.phpfreaks.com/topic/61414-solved-random-array/#findComment-305654 Share on other sites More sharing options...
zipp Posted July 23, 2007 Author Share Posted July 23, 2007 Thank you I love this place, i can always get the help I need. Link to comment https://forums.phpfreaks.com/topic/61414-solved-random-array/#findComment-305732 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.