Introduction
In Laravel 12.16, a new hasAll
method was added to the Illuminate\Support\Arr
class. The Arr::hasAll
method allows you to check if all specified keys exist in an array, making it easy to validate the presence of multiple keys at once.
The method was contributed by @devajmeireles in PR #55815.
In this Quickfire article, I'm going to show you how to use the Arr::hasAll
method in your Laravel applications.
Checking if All Keys Exist in an Array
The Arr::hasAll
method checks if all the specified keys exist in a given array. If all the keys are present, the method will return true
. Otherwise, it will return false
.
The method accepts two parameters:
-
array
- The array to check. -
keys
- A single key or an array of keys to check for existence in the array.
Let's take a look at an example of how to use this method:
1use Illuminate\Support\Arr; 2 3$user = [ 4 'name' => 'Ash Allen', 5 'location' => 'United Kingdom', 6 'role' => 'Web developer', 7]; 8 9Arr::hasAll(array: $user, keys: 'name'); // true10Arr::hasAll(array: $user, keys: ['name', 'location']); // true11 12Arr::hasAll(array: $user, keys: 'language'); // false13Arr::hasAll(array: $user, keys: ['name', 'language']); // false
As we can see in the example above, the first two calls to Arr::hasAll
return true
because the specified keys exist in the $user
array. The last two calls return false
because the language
key does not exist in the array.
Checking if Nested Keys Exist in an Array
A handy feature of the Arr::hasAll
method is that it can also check for nested keys in an array. This is particularly useful when dealing with more complex data structures.
For instance, let's take our previous example and add a nested links
array to the user data:
1use Illuminate\Support\Arr; 2 3$user = [ 4 'name' => 'Ash Allen', 5 'location' => 'United Kingdom', 6 'role' => 'Web developer', 7 'links' => [ 8 'linked_in' => 'linkedin.com/in/ashleyjcallen/', 9 'github' => 'github.com/ash-jc-allen'10 ]11];12 13Arr::hasAll(array: $user, keys: 'links.linked_in'); // true14Arr::hasAll(array: $user, keys: ['links.linked_in', 'links.github']); // true15 16Arr::hasAll(array: $user, keys: ['links.x']); // false17Arr::hasAll(array: $user, keys: ['links.linked_in', 'links.github', 'links.x']); // false
As we can see in the example above, we can use dot notation (e.g. links.linked_in
) with the Arr::hasAll
method to check nested keys. The first two calls return true
because the specified nested keys exist in the $user
array. The last two calls return false
because the links.x
key does not exist in the array.
Related Articles
You might also be interested in reading these related articles that cover similar topics about arrays in PHP:
Conclusion
In this Quickfire article, we've taken a quick look at how to use the new Arr::hasAll
method in Laravel 12.16 to check if all specified keys exist in an array. Hopefully, this method will come in handy for you in your Laravel projects.
If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀