From 6b95535b4358967c603807804bf8116a36a0c8a8 Mon Sep 17 00:00:00 2001 From: Marc van der Meulen Date: Tue, 12 Apr 2016 00:38:49 +0200 Subject: [PATCH 1/3] Remove key when key exists --- src/Illuminate/Support/Arr.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Illuminate/Support/Arr.php b/src/Illuminate/Support/Arr.php index 940978d62bb..0e11050b465 100755 --- a/src/Illuminate/Support/Arr.php +++ b/src/Illuminate/Support/Arr.php @@ -233,6 +233,11 @@ public static function forget(&$array, $keys) } foreach ($keys as $key) { + if (static::exists($array, $key)) { + unset($array[$key]); + continue; + } + $parts = explode('.', $key); // clean up before each pass From cebc939e8ea6a5a17921b729333808a9bc8e814b Mon Sep 17 00:00:00 2001 From: Marc van der Meulen Date: Tue, 12 Apr 2016 13:04:04 +0200 Subject: [PATCH 2/3] Add array pull and array forget tests --- tests/Support/SupportArrTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 4d0a7db4cb3..ea337d3fb19 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -305,6 +305,18 @@ public function testPull() $name = Arr::pull($array, 'name'); $this->assertEquals('Desk', $name); $this->assertEquals(['price' => 100], $array); + + // Only works on first level keys + $array = ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']; + $name = Arr::pull($array, 'joe@example.com'); + $this->assertEquals('Joe', $name); + $this->assertEquals(['jane@localhost' => 'Jane'], $array); + + // Does not work for nested keys + $array = ['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']]; + $name = Arr::pull($array, 'emails.joe@example.com'); + $this->assertEquals(null, $name); + $this->assertEquals(['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']], $array); } public function testSet() @@ -438,5 +450,15 @@ public function testForget() $array = ['products' => ['desk' => ['price' => 50], null => 'something']]; Arr::forget($array, ['products.amount.all', 'products.desk.price']); $this->assertEquals(['products' => ['desk' => [], null => 'something']], $array); + + // Only works on first level keys + $array = ['joe@example.com' => 'Joe', 'jane@example.com' => 'Jane']; + Arr::forget($array, 'joe@example.com'); + $this->assertEquals(['jane@example.com' => 'Jane'], $array); + + // Does not work for nested keys + $array = ['emails' => ['joe@example.com' => ['name' => 'Joe'], 'jane@localhost' => ['name' => 'Jane']]]; + Arr::forget($array, ['emails.joe@example.com','emails.jane@localhost']); + $this->assertEquals(['emails' => ['joe@example.com' => ['name' => 'Joe']]], $array); } } From 1fb37a5b73c4435e1906e2f217f93d3dc6314517 Mon Sep 17 00:00:00 2001 From: Marc van der Meulen Date: Tue, 12 Apr 2016 13:05:12 +0200 Subject: [PATCH 3/3] Fix styling --- tests/Support/SupportArrTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index ea337d3fb19..919284059d3 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -458,7 +458,7 @@ public function testForget() // Does not work for nested keys $array = ['emails' => ['joe@example.com' => ['name' => 'Joe'], 'jane@localhost' => ['name' => 'Jane']]]; - Arr::forget($array, ['emails.joe@example.com','emails.jane@localhost']); + Arr::forget($array, ['emails.joe@example.com', 'emails.jane@localhost']); $this->assertEquals(['emails' => ['joe@example.com' => ['name' => 'Joe']]], $array); } }