tibberous Posted August 26, 2012 Share Posted August 26, 2012 I am trying to get the 26 letters of the alphabet as an array in random order. Sounds easy right? I've been at it 45 minutes - still doesn't work: function random_letters(){ $letters = array(); for($i=0;$i<26;$i++){ $letters[$i] = chr($i+97); } $retval = array(); for($i=0;$i<26;$i++){ $indexes = array_keys($letters); $index = array_slice($indexes, $index = floor(rand(0,count($indexes))), 1); $index = $index[0]; $retval[$i] = $letters[$index]; $retval[$i] = $retval[$i][0]; unset($letters[$index]); } return $retval; } I've decided to say hell with it and just make a database with the 26 letters, then use "select * from letters order by rand()" But I'd like to see it done with php. Only rule is you can't use usort. Quote Link to comment https://forums.phpfreaks.com/topic/267629-how-to-get-all-the-letters-in-random-order/ Share on other sites More sharing options...
MMDE Posted August 26, 2012 Share Posted August 26, 2012 I think this would work: $string = 'someword'; $letter_array = explode('', $string); shuffle($letter_array); $string = ''; foreach($letter_array AS $letter){ $string .= $letter; } echo $string; Might be a better way? Quote Link to comment https://forums.phpfreaks.com/topic/267629-how-to-get-all-the-letters-in-random-order/#findComment-1372693 Share on other sites More sharing options...
Christian F. Posted August 26, 2012 Share Posted August 26, 2012 Slightly better, as it does away with an unnecessary loop and explode call: $lettersArray = range ('a', 'z'); shuffle ($lettersArray); $lettersRand = implode ('', $lettersArray); Quote Link to comment https://forums.phpfreaks.com/topic/267629-how-to-get-all-the-letters-in-random-order/#findComment-1372696 Share on other sites More sharing options...
MMDE Posted August 26, 2012 Share Posted August 26, 2012 Slightly better, as it does away with an unnecessary loop and explode call: $lettersArray = range ('a', 'z'); shuffle ($lettersArray); $lettersRand = implode ('', $lettersArray); Yup, I totally forgot implode. xD Quote Link to comment https://forums.phpfreaks.com/topic/267629-how-to-get-all-the-letters-in-random-order/#findComment-1372701 Share on other sites More sharing options...
tibberous Posted August 27, 2012 Author Share Posted August 27, 2012 Damn it - I wrote a whole reply and hit disappeared when I hit 'Marked Solved'. Moral: Everything should always only ever be written in ajax, ever. Quote Link to comment https://forums.phpfreaks.com/topic/267629-how-to-get-all-the-letters-in-random-order/#findComment-1372780 Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 Not sure if I agree with that statement, as it introduces a totally unnecessary HTTP call. For something that is done in 2 or 3 lines of PHP, as shown above. (I take it you actually tried my code?) Not to mention all of those who either have JS turned off or using browser that doesn't support JS/AJAX. They won't be getting your randomized letters at all, if you're relying upon JS/AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/267629-how-to-get-all-the-letters-in-random-order/#findComment-1372819 Share on other sites More sharing options...
PFMaBiSmAd Posted August 27, 2012 Share Posted August 27, 2012 Ummm, tibberous' post concerns something the forum software apparently did when he hit a menu button in it. Quote Link to comment https://forums.phpfreaks.com/topic/267629-how-to-get-all-the-letters-in-random-order/#findComment-1372822 Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 Ah, then I see. Ignore my statement then. Sorry about the confusion. Quote Link to comment https://forums.phpfreaks.com/topic/267629-how-to-get-all-the-letters-in-random-order/#findComment-1372824 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.