-
-
Save mllvzeth/d877cc6fedf511bd2b603001c215ac0d to your computer and use it in GitHub Desktop.
shuffle an array while preserving keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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