DEV Community

Cover image for Check Every Key Exists in a PHP Array with Arr::hasAll()
Ash Allen
Ash Allen

Posted on • Originally published at ashallendesign.co.uk

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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! 🚀

Top comments (1)

Collapse
 
xwero profile image
david duymelinck • Edited

If I looked at the PR the name would not been approved. But the bad naming started with has and hasAny.

I took a dive into the rabbithole aka the code.

public static function hasAll($array, $keys)
    {
        $keys = (array) $keys;

        if (! $array || $keys === []) {
            return false;
        }

        foreach ($keys as $key) {
            if (! static::has($array, $key)) {
                return false;
            }
        }

        return true;
    }
Enter fullscreen mode Exit fullscreen mode

So the check happens in the has method. And the difference between the hasAll method and the hasAny method is when the keys loop stops. For hasAny the loop stops when the first key is found.
Why is the function checking the $array and $keys values? It is time Laravel code needs to have type hinting everywhere.

public static function has($array, $keys)
    {
        $keys = (array) $keys;

        if (! $array || $keys === []) {
            return false;
        }

        foreach ($keys as $key) {
            $subKeyArray = $array;

            if (static::exists($array, $key)) {
                continue;
            }

            foreach (explode('.', $key) as $segment) {
                if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
                    $subKeyArray = $subKeyArray[$segment];
                } else {
                    return false;
                }
            }
        }

        return true;
    }
Enter fullscreen mode Exit fullscreen mode

My WTF meter went in the red. If the has method already accepts an array, wouldn't it be better to have flags to change the behaviour of the function instead of different methods?
The has method also feels messy because there is no clear separation between the regular keys and the dot notation keys.

Code like this makes me not use the helpers of Laravel.