How to check an array is associative in PHP?

Snippet

An array is "associative" if it doesn't have sequential numerical keys beginning with zero.

downloadcopy
<?php
function isAssoc(array $array){
    $keys = array_keys($array);
    return array_keys($keys) !== $keys;
}

//using
var_dump(isAssoc(['test'=>'associative','1'=>'integer'])); // true
var_dump(isAssoc(['test','integer','associative'])); // false

In case your array does not start from "0" or keys out of order, you can check all keys if they are numbers

downloadcopy
<?php
function isAssoc($a) {
    foreach(array_keys($a) as $key)
        if (!is_int($key)) return true;
    return false;
}

//using
var_dump(isAssoc(['test','integer','associative'])); // false
var_dump(isAssoc([1=>'test',2=>'integer',100=>'associative'])); // false