Skip to content

Commit

Permalink
Added Arr::shuffleAssoc support (#731)
Browse files Browse the repository at this point in the history
* Added `Arr::shuffleAssoc` support

* feat: add optional seed parameter to `Arr::shuffleAssoc`

* test: add test for Arr::shuffleAssoc with seed parameter

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Nov 4, 2024
1 parent b677459 commit ae3b1de
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions output/Hyperf/Collection/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@

class Arr
{
/**
* Shuffle an associative array.
*
* @param array $array
* @param int|null $seed
* @return array
*/
public static function shuffleAssoc($array, $seed = null)
{
}

/**
* Sort given array by many properties.
*
Expand Down
19 changes: 19 additions & 0 deletions src/ArrMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@
*/
class ArrMixin
{
public function shuffleAssoc()
{
return function ($array, $seed = null) {
if (empty($array)) {
return $array;
}

$keys = array_keys($array);
$keys = Arr::shuffle($keys, $seed);
$shuffled = [];

foreach ($keys as $key) {
$shuffled[$key] = $array[$key];
}

return $shuffled;
};
}

public function sortByMany()
{
return function ($array, $comparisons = []) {
Expand Down

0 comments on commit ae3b1de

Please sign in to comment.