In Laravel 11.x & Laravel 12.x, the `Arr::has()` method is part of the `Illuminate\Support\Arr` class and is used to check if a specific key or set of keys exists in an array. This is particularly useful when working with nested arrays, as it allows you to check for the existence of keys at any depth.
Syntax
<?php Arr::has(array $array, string|array $keys): bool ?>
->`$array`: The array to check.
->`$keys`: The key or keys to check for in the array. This can be a single key as a string or multiple keys as an array.
-> Returns: `true` if the key(s) exist, `false` otherwise.
1. Check for a Single Key
<?php use Illuminate\Support\Arr; $array = [ 'name' => 'John', 'age' => 30, ]; $hasName = Arr::has($array, 'name'); // true $hasEmail = Arr::has($array, 'email'); // false ?>
2. Check for Multiple Keys
<?php use Illuminate\Support\Arr; $array = [ 'name' => 'John', 'age' => 30, 'email' => 'john@example.com', ]; $hasKeys = Arr::has($array, ['name', 'email']); // true $hasKeys = Arr::has($array, ['name', 'address']); // false (because 'address' is missing) ?>
3. Check for Nested Keys
<?php use Illuminate\Support\Arr; $array = [ 'user' => [ 'name' => 'John', 'address' => [ 'city' => 'New York', ], ], ]; $hasCity = Arr::has($array, 'user.address.city'); // true $hasCountry = Arr::has($array, 'user.address.country'); // false ?>
4. Check for Wildcard Keys
You can use dot notation to check for nested keys dynamically.
<?php use Illuminate\Support\Arr; $array = [ 'users' => [ ['name' => 'John'], ['name' => 'Jane'], ], ]; $hasFirstUserName = Arr::has($array, 'users.0.name'); // true $hasSecondUserEmail = Arr::has($array, 'users.1.email'); // false ?>