has($array, $key)
The has helper method will return a boolean value that indicates if the given $key exists in the supplied $array, using dot notation. This method is incredibly useful when checking if an array has a specific key at an arbitrary depth.
Considering the following array:
1<?php
2
3$anArray = [
4 'nested_array_one' => [
5 'nested_array_two' => [
6 'key' => 'value'
7 ]
8 ]
9];
We can determine that the nested key actually does exist:
1<?php
2
3use Illuminate\Support\Arr;
4
5$doesExist = Arr::has($anArray, 'nested_array_one.nested_array_two.key');
Because the nested key key does in fact exist, the value of $doesExist would be true. Inversely, we can prove that a key of does_not_exist does not, in fact, exist:
1<?php
2
3use Illuminate\Support\Arr;
4
5$doesExist = Arr::has($anArray, 'does_not_exist');
At this point, the value of $doesExist would be false, which is accurate.
Using this method is easier to read than using PHP's isset function and checking at each level of the array whether a particular key exists.
#array_has($array, $key)
The array_has function is a shortcut to calling Arr::has. This function is declared in the global namespace.
∎