mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 19:27:39 +08:00
Added Hyperf\Collection\Arr::shuffleAssoc
method (#7141)
This commit is contained in:
parent
868d333f01
commit
913cd40e86
@ -1,5 +1,9 @@
|
||||
# v3.1.45 - TBD
|
||||
|
||||
## Added
|
||||
|
||||
- [#7141](https://github.com/hyperf/hyperf/pull/7141) Added method `Hyperf\Collection\Arr::shuffleAssoc`.
|
||||
|
||||
## Fixed
|
||||
|
||||
- [#7133](https://github.com/hyperf/hyperf/pull/7133) Fixed bug that the connection of migrations cannot work excepted.
|
||||
|
@ -490,6 +490,26 @@ class Arr
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuffle an associative array.
|
||||
*/
|
||||
public static function shuffleAssoc(array $array, ?int $seed = null): array
|
||||
{
|
||||
if (empty($array)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$keys = array_keys($array);
|
||||
$keys = static::shuffle($keys, $seed);
|
||||
$random = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$random[$key] = $array[$key];
|
||||
}
|
||||
|
||||
return $random;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the array using the given callback or "dot" notation.
|
||||
*/
|
||||
|
@ -228,6 +228,81 @@ class ArrTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testShuffleAssoc(): void
|
||||
{
|
||||
$source = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
|
||||
|
||||
$shuffled = Arr::shuffleAssoc($source);
|
||||
|
||||
$this->assertNotSame($source, $shuffled);
|
||||
$this->assertSameSize($source, $shuffled);
|
||||
$this->assertSameSize($source, array_intersect_assoc($source, $shuffled));
|
||||
|
||||
$source = ['a', 'b', 'c', 'd'];
|
||||
|
||||
$shuffled = Arr::shuffleAssoc($source);
|
||||
|
||||
$this->assertNotSame($source, $shuffled);
|
||||
$this->assertSameSize($source, $shuffled);
|
||||
$this->assertSameSize($source, array_intersect_assoc($source, $shuffled));
|
||||
|
||||
$source = [1, 2, 3, 4];
|
||||
|
||||
$shuffled = Arr::shuffleAssoc($source);
|
||||
|
||||
$this->assertNotSame($source, $shuffled);
|
||||
$this->assertSameSize($source, $shuffled);
|
||||
$this->assertSameSize($source, array_intersect_assoc($source, $shuffled));
|
||||
}
|
||||
|
||||
public function testShuffleAssocWithSeed(): void
|
||||
{
|
||||
$source = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
|
||||
|
||||
$this->assertSame(
|
||||
Arr::shuffleAssoc($source, 1234),
|
||||
Arr::shuffleAssoc($source, 1234)
|
||||
);
|
||||
$this->assertNotSame(
|
||||
Arr::shuffleAssoc($source),
|
||||
Arr::shuffleAssoc($source)
|
||||
);
|
||||
$this->assertNotSame(
|
||||
$source,
|
||||
Arr::shuffleAssoc($source, 1234)
|
||||
);
|
||||
|
||||
$source = ['a', 'b', 'c', 'd'];
|
||||
|
||||
$this->assertSame(
|
||||
Arr::shuffleAssoc($source, 1234),
|
||||
Arr::shuffleAssoc($source, 1234)
|
||||
);
|
||||
$this->assertNotSame(
|
||||
Arr::shuffleAssoc($source),
|
||||
Arr::shuffleAssoc($source)
|
||||
);
|
||||
$this->assertNotSame(
|
||||
$source,
|
||||
Arr::shuffleAssoc($source, 1234)
|
||||
);
|
||||
|
||||
$source = [1, 2, 3, 4];
|
||||
|
||||
$this->assertSame(
|
||||
Arr::shuffleAssoc($source, 1234),
|
||||
Arr::shuffleAssoc($source, 1234)
|
||||
);
|
||||
$this->assertNotSame(
|
||||
Arr::shuffleAssoc($source),
|
||||
Arr::shuffleAssoc($source)
|
||||
);
|
||||
$this->assertNotSame(
|
||||
$source,
|
||||
Arr::shuffleAssoc($source, 1234)
|
||||
);
|
||||
}
|
||||
|
||||
public function testEmptyShuffle(): void
|
||||
{
|
||||
$this->assertEquals([], Arr::shuffle([]));
|
||||
|
Loading…
Reference in New Issue
Block a user