Support the ongoing development of Laravel.io →
Article Hero Image

Check Every Key Exists in a PHP Array with Arr::hasAll()

10 Jun, 2025 3 min read

Photo by mostafa meraji on Unsplash

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:

use Illuminate\Support\Arr;

$user = [
  'name' => 'Ash Allen',
  'location' => 'United Kingdom',
  'role' => 'Web developer',
];

Arr::hasAll(array: $user, keys: 'name'); // true
Arr::hasAll(array: $user, keys: ['name', 'location']); // true

Arr::hasAll(array: $user, keys: 'language'); // false
Arr::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:

use Illuminate\Support\Arr;

$user = [
  'name' => 'Ash Allen',
  'location' => 'United Kingdom',
  'role' => 'Web developer',
  'links' => [
    'linked_in' => 'linkedin.com/in/ashleyjcallen/',
    'github' => 'github.com/ash-jc-allen'
  ]
];

Arr::hasAll(array: $user, keys: 'links.linked_in'); // true
Arr::hasAll(array: $user, keys: ['links.linked_in', 'links.github']); // true

Arr::hasAll(array: $user, keys: ['links.x']); // false
Arr::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.

Keep on building awesome stuff! 🚀

Last updated 1 day ago.

driesvints liked this article

1
Like this article? Let the author know and give them a clap!
ash-jc-allen (Ash Allen) I'm a freelance Laravel web developer from Preston, UK. I maintain the Ash Allen Design blog and get to work on loads of cool and exciting projects 🚀

Other articles you might like

Article Hero Image June 13th 2025

Formatting Monetary Values in JavaScript

Introduction When building your web applications, you might need to format numbers as monetary value...

Read article
Article Hero Image June 10th 2025

Cast Laravel Model Fields to "Illuminate\Support\Uri" with "AsUri"

Introduction I recently contributed a new feature to the Laravel framework that allows you to cast m...

Read article
Article Hero Image May 16th 2025 Sponsored

Full Text Search & More with Typesense and Laravel

Building powerful and user-friendly search functionality is crucial for many Laravel applications. W...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.