Search

Laravel 5: Checking if an Array is an Associative Array With isAssoc

April 11, 2018 —John Koster

The isAssoc helper method can be used to determine if a given $array is an associative array or not. The provided documentation for the isAssoc method states that an array is associative if it does not have sequential numeric keys beginning with zero.

#Signature

The signature of the isAssoc method is:

1public static function isAssoc(
2 array $array
3);

#Example Use

However, in practice the actual results are slightly more nuanced:

1use Illuminate\Support\Arr;
2 
3// false
4$isAssoc = Arr::isAssoc([0,1,2,3]);
5 
6// false
7$isAssoc = Arr::isAssoc([1,2,3,4]);
8 
9// false
10$isAssoc = Arr::isAssoc([2,3,4,6]);
11 
12// true
13$isAssoc = Arr::isAssoc([
14 'associative' => 'array'
15]);
16 
17// false
18$isAssoc = Arr::isAssoc([
19 0 => 'A',
20 1 => 'B',
21 2 => 'C'
22]);

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.