sacox Posted January 11, 2010 Share Posted January 11, 2010 Suppose I have this array 1,A 2,B 3,C 4,D 5,E How to random the array but only the numeric index (1 to 5) Quote Link to comment https://forums.phpfreaks.com/topic/188029-how-to-get-random-value-from-array-2-dimentions/ Share on other sites More sharing options...
RichardRotterdam Posted January 11, 2010 Share Posted January 11, 2010 Suppose I have this array 1,A 2,B 3,C 4,D 5,E How to random the array but only the numeric index (1 to 5) Did you mean that you have an array like the following (regardless of how the code is written) <?php $arr = array( 1 => "A", 2 => "B", 3 => "C", 4 => "D", 5 => "E" ); Quote Link to comment https://forums.phpfreaks.com/topic/188029-how-to-get-random-value-from-array-2-dimentions/#findComment-992715 Share on other sites More sharing options...
sacox Posted January 11, 2010 Author Share Posted January 11, 2010 it is 2 dimentions array like this $data=array( 0=>array("1","A"), 1=>array("2","B"), 2=>array("3","C"), 3=>array("4","D"), 4=>array("5","E") ) Quote Link to comment https://forums.phpfreaks.com/topic/188029-how-to-get-random-value-from-array-2-dimentions/#findComment-992718 Share on other sites More sharing options...
trq Posted January 11, 2010 Share Posted January 11, 2010 And what part then do you want randomized? You might simply be looking for.... shuffle($data) which will randomize the first dimension. Quote Link to comment https://forums.phpfreaks.com/topic/188029-how-to-get-random-value-from-array-2-dimentions/#findComment-992720 Share on other sites More sharing options...
cags Posted January 11, 2010 Share Posted January 11, 2010 There is the shuffle function, you didn't exactly give many details so I'm not sure if that's appropriate. Quote Link to comment https://forums.phpfreaks.com/topic/188029-how-to-get-random-value-from-array-2-dimentions/#findComment-992721 Share on other sites More sharing options...
sacox Posted January 11, 2010 Author Share Posted January 11, 2010 There is the shuffle function, you didn't exactly give many details so I'm not sure if that's appropriate. I'm sorry it's my mistake, I have this script to get 3 unique random data out of 10 $array = range(0, 10); $keys = array_rand($array, 3); echo $array[$keys[0]]; echo "<br />"; echo $array[$keys[1]]; echo "<br />"; echo $array[$keys[2]]; echo "<br />"; and now I want to use 2 dimensions array $data=array( 0=>array("1","A"), 1=>array("2","B"), 2=>array("3","C"), 3=>array("4","D"), 4=>array("5","E") ) how to get 2 unique random data from it? Quote Link to comment https://forums.phpfreaks.com/topic/188029-how-to-get-random-value-from-array-2-dimentions/#findComment-992724 Share on other sites More sharing options...
RichardRotterdam Posted January 11, 2010 Share Posted January 11, 2010 What is it you want to display? without that being clear it's hard for someone to help you out Quote Link to comment https://forums.phpfreaks.com/topic/188029-how-to-get-random-value-from-array-2-dimentions/#findComment-992761 Share on other sites More sharing options...
ignace Posted January 11, 2010 Share Posted January 11, 2010 $keys = array_rand($array, 2); echo $array[$keys[0]][0]; // 1 echo $array[$keys[1]][1]; // E Quote Link to comment https://forums.phpfreaks.com/topic/188029-how-to-get-random-value-from-array-2-dimentions/#findComment-992847 Share on other sites More sharing options...
sacox Posted January 12, 2010 Author Share Posted January 12, 2010 thanks ignace i got the idea Quote Link to comment https://forums.phpfreaks.com/topic/188029-how-to-get-random-value-from-array-2-dimentions/#findComment-993268 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.