diff --git a/src/Illuminate/Support/Arr.php b/src/Illuminate/Support/Arr.php index 5edda9ba9820..616c030c5fe3 100755 --- a/src/Illuminate/Support/Arr.php +++ b/src/Illuminate/Support/Arr.php @@ -541,11 +541,20 @@ public static function set(&$array, $key, $value) * Shuffle the given array and return the result. * * @param array $array + * @param int|null $seed * @return array */ - public static function shuffle($array) + public static function shuffle($array, $seed = null) { - shuffle($array); + if (is_null($seed)) { + shuffle($array); + } else { + srand($seed); + + usort($array, function () { + return rand(-1, 1); + }); + } return $array; } diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index a20d39d5d894..86dbacd23eee 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -1360,19 +1360,7 @@ public function shift() */ public function shuffle($seed = null) { - $items = $this->items; - - if (is_null($seed)) { - shuffle($items); - } else { - srand($seed); - - usort($items, function () { - return rand(-1, 1); - }); - } - - return new static($items); + return new static(Arr::shuffle($this->items, $seed)); } /** diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index d62da7359398..59f0cc4c404c 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -498,6 +498,14 @@ public function testSet() $this->assertEquals(['products' => ['desk' => ['price' => 200]]], $array); } + public function testShuffleWithSeed() + { + $this->assertEquals( + Arr::shuffle(range(0, 100, 10), 1234), + Arr::shuffle(range(0, 100, 10), 1234) + ); + } + public function testSort() { $unsorted = [