devWhiz Posted May 28, 2011 Share Posted May 28, 2011 I dont know how to word this really. what I want to do is $Var[] = 3; $Var[] = 6; $Var[] = 2; $Var[] = 17; for($i=0; $i!=count($Var); $i++) { echo "#$i: ".$Var[$i]."\n"; } I know I might be able to use rand() but I need it to echo ALL of the variables in the array in a random order, I know if I use count to find out how many variables there are that it would echo them 4 times randomly but might echo the same variable 2 times making it to where it only echos 3 different varables, if I am being confusing please let me know, I just want to get this resolved, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/ Share on other sites More sharing options...
xyph Posted May 28, 2011 Share Posted May 28, 2011 shuffle() the array. Then just foreach. Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/#findComment-1221621 Share on other sites More sharing options...
devWhiz Posted May 28, 2011 Author Share Posted May 28, 2011 Yeah I was looking at shuffle, didnt know if there was a better way or something, this will work for now, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/#findComment-1221624 Share on other sites More sharing options...
xyph Posted May 28, 2011 Share Posted May 28, 2011 Dunno how anything could be better than shuffle. It does exactly what you want. Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/#findComment-1221626 Share on other sites More sharing options...
Alex1646 Posted May 28, 2011 Share Posted May 28, 2011 Yeah, Shuffle() do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/#findComment-1221634 Share on other sites More sharing options...
devWhiz Posted May 28, 2011 Author Share Posted May 28, 2011 what about this for($abc=1; $abc<25; $abc++) { $ABC1 = rand($abc); $ABC1 = $ABC2; echo $ABC1 . " " . $ABC2."\n"; } when I want to randomize abc1 but I want it to make abc2 randomized also but not different than abc1 Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/#findComment-1221637 Share on other sites More sharing options...
xyph Posted May 28, 2011 Share Posted May 28, 2011 What? Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/#findComment-1221639 Share on other sites More sharing options...
mikesta707 Posted May 28, 2011 Share Posted May 28, 2011 that... doesn't make any sense.. where did that come from Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/#findComment-1221642 Share on other sites More sharing options...
devWhiz Posted May 28, 2011 Author Share Posted May 28, 2011 lol oops, how can I randomize the number of the for loop? like for($abc=1; $abc!=25; $abc++) { echo $array[$abc][0]. " " .$array[$abc][1]; } I need to randomize abc but it has to be the same for each time it echos the array, make sense? Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/#findComment-1221645 Share on other sites More sharing options...
xyph Posted May 28, 2011 Share Posted May 28, 2011 Like this? <?php $length = rand(5,25); for( $i=0;$i<$length;$i++ ) { echo $i . '<br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237721-random-array/#findComment-1221659 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.