Skip to content

Instantly share code, notes, and snippets.

@mllavez
Forked from Quinten/shuffle_assoc
Created April 6, 2017 18:20
Show Gist options
  • Save mllavez/d877cc6fedf511bd2b603001c215ac0d to your computer and use it in GitHub Desktop.
Save mllavez/d877cc6fedf511bd2b603001c215ac0d to your computer and use it in GitHub Desktop.
shuffle an array while preserving keys
<?php
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = $list[$key];
return $random;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment