Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Forget key when key exists #13121

Merged
merged 3 commits into from
Apr 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}