Arr::has
Checks whether one or more dot-notation keys exist in an array, counting null values as present.
public static function has($array, $keys)- Added in Laravel 5.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$$array | \ArrayAccess|array | The array or ArrayAccess object to inspect. An empty array always yields false. |
$$keys | string|array | A single dot-notation key, or an array of keys that must all be present. |
Return value
boolTrue only when every given key exists, false otherwise.
Arr::has answers whether a dot-notation path exists. Existence is decided with array_key_exists at every segment, so a key whose value is null counts as present — the opposite of isset() and of Arr::get(...) !== null.
When to reach for it
Use it when the presence of a field is itself meaningful: a PATCH body where null means "clear this column" and an absent key means "leave it alone", a config check where a null value is a deliberate setting, or a payload guard before you read several related fields.
What trips people up
When you pass an array of keys the result is an and: it is true only if every key is present, so one missing key makes the whole call false. If you want "any of these", use Arr::hasAny instead. Two edge cases return false without checking anything: an empty array, and an empty key list. And as with Arr::get, there is no wildcard, and a literal dotted key is tested before the path is split.
Example
When to use it
- Distinguishing "field sent as null" from "field not sent" in a PATCH payload.
- Guarding a block of code that reads several related nested keys.
- Asserting that a config array contains every key an integration needs.
Frequently asked questions
Does Arr::has return true for a key whose value is null?
Yes, existence is checked with array_key_exists, so a stored null counts as present. That is the key difference from isset().
What happens when I pass an array of keys to Arr::has?
It returns true only if every key exists, behaving as a logical and. Use Arr::hasAny when any single key should be enough.
Why does Arr::has return false on an empty array?
The method short-circuits to false when the array is empty or the key list is empty, before any key is examined.
More examples
Null means clear, absent means leave alone
Prints {"name":"Ada","nickname":null}. The nickname was explicitly sent as null and is kept as a change that will clear the column, while email was never sent and is left out entirely. An isset() check would have dropped nickname along with email.
<?php
use Illuminate\Support\Arr;
$patch = ['nickname' => null, 'name' => 'Ada'];
$changes = [];
foreach (['name', 'nickname', 'email'] as $field) {
if (Arr::has($patch, $field)) {
$changes[$field] = $patch[$field];
}
}
echo json_encode($changes), "\n";All keys versus any key
Prints bool(false) then bool(true). Arr::has requires every listed key, which is what you want when validating that a config block is complete; Arr::hasAny is the or version and is the right choice when any one of several aliases will do.
<?php
use Illuminate\Support\Arr;
$config = ['mail' => ['host' => 'localhost']];
var_dump(Arr::has($config, ['mail.host', 'mail.port']));
var_dump(Arr::hasAny($config, ['mail.host', 'mail.port']));A literal dotted key is checked first
Prints bool(true) then bool(false). The full string is tested as a key before it is split on dots, so a flat array of dotted keys reports true for its own keys but false for their prefixes. Run the array through Arr::undot first if you need to probe intermediate levels.
<?php
use Illuminate\Support\Arr;
$array = ['a.b' => 1];
var_dump(Arr::has($array, 'a.b'));
var_dump(Arr::has($array, 'a'));
Discussion