shuffle array with keys

The php4 version of shuffle does not keep the array keys intact, here is a nifty function to add to your list

if (function_exists('shuffle_with_keys')===false) {
  function shuffle_with_keys(&$array) {
	/* Auxiliary array to hold the new order */
	$aux = array();
	/* We work with an array of the keys */
	$keys = array_keys($array);
	/* We shuffle the keys */
	shuffle($keys);
	/* We iterate thru' the new order of the keys */
	foreach($keys as $key) {
	  /* We insert the key, value pair in its new order */
	  $aux[$key] = $array[$key];
	  /* We remove the element from the old array to save memory */
	  unset($array[$key]);
	}
	/* The auxiliary array with the new order overwrites the old variable */
	$array = $aux;
  }
}
Comment are closed.