Michdd Posted November 8, 2008 Share Posted November 8, 2008 Why is this outputting the same thing everytime? <?php $arr = array('1','2'); echo shuffle($arr); ?> It's outputting 1, everytime, is that the correct way to use it? Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/ Share on other sites More sharing options...
DeanWhitehouse Posted November 8, 2008 Share Posted November 8, 2008 echo shuffle($arr[0]); Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685717 Share on other sites More sharing options...
Michdd Posted November 8, 2008 Author Share Posted November 8, 2008 Warning: shuffle() expects parameter 1 to be array, string given in /home/a3496022/public_html/shuffle.php on line 5 Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685718 Share on other sites More sharing options...
DeanWhitehouse Posted November 8, 2008 Share Posted November 8, 2008 echo shuffle($arr[1]); maybe? Sorry i haven't used it before Edit: try <?php $arr = array('1','2'); $arr = shuffle($arr); echo $arr[0]; ?> Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685719 Share on other sites More sharing options...
Michdd Posted November 8, 2008 Author Share Posted November 8, 2008 echo shuffle($arr[1]); maybe? Sorry i haven't used it before Edit: try <?php $arr = array('1','2'); $arr = shuffle($arr); echo $arr[0]; ?> First thing outputs the same error. Second thing echos nothing. Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685720 Share on other sites More sharing options...
wildteen88 Posted November 8, 2008 Share Posted November 8, 2008 shuffle doesn't return anythink, accept true (or 1). The way you use suffle is $arr = range(0, 100); echo 'Before<pre>' . print_r($arr, true) .'</pre>'; shuffle($arr); echo 'After<pre>' . print_r($arr, true) .'</pre>'; Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685722 Share on other sites More sharing options...
Mchl Posted November 8, 2008 Share Posted November 8, 2008 shuffle() indeed takes an array as an argument. But I think you should randomize random seed before calling it. See how it's used in manual. See srand() function before shuffle() ? And besides, echoing it, will always return 1 because it returns TRUE when it executes properly. shuffle($arr); means that $arr after that has been shuffled. So $arr = array("0","1","2","3"); shuffle($arr); print_r($arr); //should print somewhat shuffled array Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685723 Share on other sites More sharing options...
Michdd Posted November 8, 2008 Author Share Posted November 8, 2008 I'm not actually looking for that, I'm looking for something that will give me a random thing from the array, I was pointed in the direction of shuffle() is this not good for it? Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685729 Share on other sites More sharing options...
wildteen88 Posted November 8, 2008 Share Posted November 8, 2008 Look into http://uk.php.net/array_rand Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685730 Share on other sites More sharing options...
Michdd Posted November 8, 2008 Author Share Posted November 8, 2008 Look into http://uk.php.net/array_rand Works good, just one more question, if I input things into the array more than once, that just gives it a higher probability of it being echoed, correct? Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685732 Share on other sites More sharing options...
DeanWhitehouse Posted November 8, 2008 Share Posted November 8, 2008 Yes Link to comment https://forums.phpfreaks.com/topic/131967-solved-shuffle/#findComment-685737 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.