Jump to content

How to get all the letters in random order?


tibberous

Recommended Posts

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.

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.