From 19e41c266b11de2ee180fb6d348129e911624e89 Mon Sep 17 00:00:00 2001 From: xamgore <xamgore@ya.ru> Date: Mon, 6 Jul 2015 00:03:02 +0300 Subject: [PATCH] New function for shuffling associative arrays There is a problem at the standard (STL) realization: it doesn't work with assoc arrays, so [0 => 123, 5 => 123] is transformed into [0 => 123, 1 => 123]. `Arr::shuffle_assoc` saves keys. --- ~dev_rating/system/classes/Kohana/Arr.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/~dev_rating/system/classes/Kohana/Arr.php b/~dev_rating/system/classes/Kohana/Arr.php index 4876fe6e6..27b110246 100644 --- a/~dev_rating/system/classes/Kohana/Arr.php +++ b/~dev_rating/system/classes/Kohana/Arr.php @@ -10,7 +10,22 @@ */ class Kohana_Arr { - /** + public static function shuffle_assoc($list) { + if (!is_array($list)) + return $list; + + $keys = array_keys($list); + shuffle($keys); + + $random = []; + foreach ($keys as &$key) { + $random[$key] = $list[$key]; + } + + return $random; + } + + /** * @var string default delimiter for path() */ public static $delimiter = '.'; -- GitLab