Posted By

breakerjump on 10/20/08


Tagged

array shuffle keys Randomize preserve


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

luman
pecta


Shuffle/Randomize Array while Preserving Keys


 / Published in: PHP
 

URL: http://us3.php.net/manual/en/function.shuffle.php#83007

  1. function shuffle_with_keys(&$array) {
  2. $aux = array();
  3. $keys = array_keys($array);
  4. shuffle($keys);
  5. foreach($keys as $key) {
  6. $aux[$key] = $array[$key];
  7. unset($array[$key]);
  8. }
  9. $array = $aux;
  10. }

Report this snippet  

You need to login to post a comment.