Fixed bug that Arr::forget failed when the integer key does not exists. (#3348)

* Fixed bug that Arr::forget failed when the integer key does not exists.

* Added test cases
This commit is contained in:
李铭昕 2021-03-08 18:32:08 +08:00 committed by GitHub
parent 48b319e2f0
commit 3f26863692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 1 deletions

View File

@ -1,5 +1,9 @@
# v2.1.10 - TBD
## Fixed
- [#3348](https://github.com/hyperf/hyperf/pull/3348) Fixed bug that `Arr::forget` failed when the integer key does not exists.
# v2.1.9 - 2021-03-08
## Fixed

View File

@ -205,7 +205,7 @@ class Arr
unset($array[$key]);
continue;
}
$parts = explode('.', $key);
$parts = explode('.', (string) $key);
// clean up before each pass
$array = &$original;
while (count($parts) > 1) {

View File

@ -141,4 +141,33 @@ class ArrTest extends TestCase
$result = Arr::merge($result, $array2);
$this->assertSame($array1, $result);
}
public function testArrorForget()
{
$data = [1, 2];
Arr::forget($data, [1]);
$this->assertSame([1], $data);
$data = ['id' => 1, 'name' => 'Hyperf'];
Arr::forget($data, ['gender']);
$this->assertSame(['id' => 1, 'name' => 'Hyperf'], $data);
Arr::forget($data, ['id']);
$this->assertSame(['name' => 'Hyperf'], $data);
$data = ['id' => 1, 'name' => 'Hyperf', 'data' => ['id' => 2], 'data.name' => 'Swoole'];
Arr::forget($data, ['data.gender']);
$this->assertSame(['id' => 1, 'name' => 'Hyperf', 'data' => ['id' => 2], 'data.name' => 'Swoole'], $data);
Arr::forget($data, ['data.name']);
$this->assertSame(['id' => 1, 'name' => 'Hyperf', 'data' => ['id' => 2]], $data);
Arr::forget($data, ['data.id']);
$this->assertSame(['id' => 1, 'name' => 'Hyperf', 'data' => []], $data);
$data = ['data' => ['data' => ['id' => 1, 'name' => 'Hyperf']]];
Arr::forget($data, ['data.data.id']);
$this->assertSame(['data' => ['data' => ['name' => 'Hyperf']]], $data);
$data = [1, 2];
Arr::forget($data, [2]);
$this->assertSame([1, 2], $data);
}
}