ecopetition Posted December 30, 2008 Share Posted December 30, 2008 Hello, I was wondering if anyone can help me with the following array: array( '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11', '12' => '12', '13' => '13', '14' => '14', '15' => '15', '16' => '16', '17' => '17', '18' => '18', '19' => '19', '20' => '20', '21' => '21', '22' => '22', '23' => '23', '24' => '24', '25' => '25', ) Now, what I want to do is select a specified number of random values from this array, say 10, and arrange them like so: 5, 8, 12, 13, 15, 18, 19, 21, 23, 25 (where a comma is the delimiter). The fact that the numbers are in order is not important but it is important that no number is repeated. Also, every time the script is refreshed, it is important that the random values would be reselected. I'd appreciate any help with this at all. Thanks, Peter Link to comment https://forums.phpfreaks.com/topic/138891-solved-selecting-random-values-from-arrays/ Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 Use shuffle, array_slice, and implode <?php $nums = range(1,25); shuffle($nums); echo implode(', ',array_slice($nums,0,10)); ?> Ken Link to comment https://forums.phpfreaks.com/topic/138891-solved-selecting-random-values-from-arrays/#findComment-726284 Share on other sites More sharing options...
ecopetition Posted December 30, 2008 Author Share Posted December 30, 2008 Thanks. Now what if I was to do it so that the array was made of text rather than numbers, eg: array( '1' => 'cheese', '2' => 'chocolate', '3' => 'butter', '4' => 'milk', '5' => 'grapes', '6' => 'bread', '7' => 'wheat', '8' => 'rice', '9' => 'spice', [etc] ) Link to comment https://forums.phpfreaks.com/topic/138891-solved-selecting-random-values-from-arrays/#findComment-726291 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 Same thing. Try it. Just don't use the range function. Ken Link to comment https://forums.phpfreaks.com/topic/138891-solved-selecting-random-values-from-arrays/#findComment-726297 Share on other sites More sharing options...
flyhoney Posted December 30, 2008 Share Posted December 30, 2008 Simpler to use http://us2.php.net/array_rand Link to comment https://forums.phpfreaks.com/topic/138891-solved-selecting-random-values-from-arrays/#findComment-726303 Share on other sites More sharing options...
flyhoney Posted December 30, 2008 Share Posted December 30, 2008 <?php $stuff = array( '1' => 'cheese', '2' => 'chocolate', '3' => 'butter', '4' => 'milk', '5' => 'grapes', '6' => 'bread', '7' => 'wheat', '8' => 'rice', '9' => 'spice' ); $rand_key = array_rand($stuff); $rand_element = $stuff[$rand_key]; ?> Link to comment https://forums.phpfreaks.com/topic/138891-solved-selecting-random-values-from-arrays/#findComment-726304 Share on other sites More sharing options...
ecopetition Posted December 30, 2008 Author Share Posted December 30, 2008 Ok thanks flyhoney. Link to comment https://forums.phpfreaks.com/topic/138891-solved-selecting-random-values-from-arrays/#findComment-726307 Share on other sites More sharing options...
kenrbnsn Posted December 30, 2008 Share Posted December 30, 2008 Using array_rand does not guarantee that you won't get repeats. Using shuffle will guarantee that. Ken Link to comment https://forums.phpfreaks.com/topic/138891-solved-selecting-random-values-from-arrays/#findComment-726404 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.