November 20, 2016 —John Koster
isAssoc(array $array)
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. However, in practice the actual results are slightly more nuanced:
1<?php 2 3use Illuminate\Support\Arr; 4 5// false 6$isAssoc = Arr::isAssoc([0,1,2,3]); 7 8// false 9$isAssoc = Arr::isAssoc([1,2,3,4]);10 11// false12$isAssoc = Arr::isAssoc([2,3,4,6]);13 14// true15$isAssoc = Arr::isAssoc([16 'associative' => 'array'17]);18 19// false20$isAssoc = Arr::isAssoc([21 0 => 'A',22 1 => 'B',23 2 => 'C'24]);
∎
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.