Tiffany B. Brown

a mish-mosh of stuff

PHP: Sorting arrays randomly with array_randsort()

Or, you know, just use the shuffle() function. Kim points out the drawbacks of shuffle in the comments below.

A custom PHP function. This function will return an array that’s been resorted in a random order. Supports both numerically-indexed and associative arrays. Uses PHP’s native array_rand() function.

function array_randsort($array,$preserve_keys=false){
	/*-------------------------------------/
	Preserving the keys works best with associative arrays.
	If you choose to preserve keys on a numerically-indexed or
    mixed-indexed array, use a foreach loop rather than a for loop
    to preserve the sorted order.
	/-------------------------------------*/

	if(!is_array($array)):
		exit('Supplied argument is not a valid array.');
	else:
		$i = NULL;

		// how long is the array?
		$array_length = count($array); 

		// Sorts the array keys in a random order.
		$randomize_array_keys = array_rand($array,$array_length);

		// if we are preserving the keys ...
		if($preserve_keys===true) {
			// reorganize the original array in a new array
			foreach($randomize_array_keys as $k=>$v){
				$randsort[$randomize_array_keys[$k]] = $array[$randomize_array_keys[$k]];
			}
		} else {
			// reorganize the original array in a new array
			for($i=0; $i < $array_length; $i++){
				$randsort[$i] = $array[$randomize_array_keys[$i]];
			}
		}
		return $randsort;
	endif;
}
  • Kim Winston-Jackson

    Pretty cool function. Are you writing a blog engine, webapp, or just using this code as a part of a function library or class? (I'm just curious:) Have you tried the shuffle() function? It randomizes arrays also, but destroys the keys in associative arrays . The other drawback of shuffle() is that it performs its tasks on the actual reference array parameter. So for data integrity a solution like yours is probably better.

    By the way, I just found your blog and really like it. I particularly enjoyed your Nov. 3, 2008 article on frameworks and your Dec. 14, 2008 article on pagination and of course this one.

  • Kim Winston-Jackson

    Pretty cool function. Are you writing a blog engine, webapp, or just using this code as a part of a function library or class? (I'm just curious:) Have you tried the shuffle() function? It randomizes arrays also, but destroys the keys in associative arrays . The other drawback of shuffle() is that it performs its tasks on the actual reference array parameter. So for data integrity a solution like yours is probably better.

    By the way, I just found your blog and really like it. I particularly enjoyed your Nov. 3, 2008 article on frameworks and your Dec. 14, 2008 article on pagination and of course this one.

  • http://www.intensedebate.com/people/tiffanybbrown tiffanybbrown

    I am doing a project for work using the CodeIgniter framework. This is one of the "helper" functions.

    Thanks for pointing out the shuffle() function. I clearly didn't look closely enough at the PHP documentation. I even remember thinking "Why doesn't PHP have a native function for this?" Good to know it does.

    In some of the instances I'm using it, shuffle() would work just fine. Although this function works well if you want to retain the associative keys. I will add a third parameter that will specify how many random items to return from an array.

    Thanks for the compliments too :-) .

  • http://www.intensedebate.com/people/tiffanybbrown tiffanybbrown

    I am doing a project for work using the CodeIgniter framework. This is one of the "helper" functions.

    Thanks for pointing out the shuffle() function. I clearly didn't look closely enough at the PHP documentation. I even remember thinking "Why doesn't PHP have a native function for this?" Good to know it does.

    In some of the instances I'm using it, shuffle() would work just fine. Although this function works well if you want to retain the associative keys. I will add a third parameter that will specify how many random items to return from an array.

    Thanks for the compliments too :-) .