diff --git a/src/Illuminate/Support/Arr.php b/src/Illuminate/Support/Arr.php index 940978d62bb1..0e11050b4655 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 diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 4d0a7db4cb38..919284059d35 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); } }