In Laravel 11.x, you can use `Arr::forget()` to remove one or multiple keys from an array, including nested keys.
Syntax
Arr::forget(array &$array, string|array $keys)
`$array` → The array from which you want to remove elements.
`$keys` → The key(s) to forget (can be a string or an array).
Example 1: Remove a Single Key
use Illuminate\Support\Arr; $data = ['name' => 'John', 'email' => 'john@example.com', 'age' => 30]; Arr::forget($data, 'email'); print_r($data);
Output:-
Array ( [name] => John [age] => 30 )
Example 2: Remove Multiple Keys
use Illuminate\Support\Arr; $data = ['name' => 'John', 'email' => 'john@example.com', 'age' => 30]; Arr::forget($data, ['name', 'age']); print_r($data);
Output:-
Array ( [email] => john@example.com )
Example 3: Remove Nested Keys
use Illuminate\Support\Arr; $data = [ 'user' => [ 'name' => 'John', 'email' => 'john@example.com', 'address' => [ 'city' => 'New York', 'zip' => '10001' ] ] ]; Arr::forget($data, 'user.address.city'); print_r($data);
Output:-
Array ( [user] => Array ( [name] => John [email] => john@example.com [address] => Array ( [zip] => 10001 ) ) )