I have these simple functions in my handy PHP tool bag:
function is_flat_array($ar) {
if (!is_array($ar))
return false;
$keys = array_keys($ar);
return array_keys($keys) === $keys;
}
function is_hash($ar) {
if (!is_array($ar))
return false;
$keys = array_keys($ar);
return array_keys($keys) !== $keys;
}
I have never tested my performance on large arrays. I mainly use it on arrays with 10 or less keys, so this is usually not a problem. I suspect it will have better performance than comparing $ keys with the span generated 0..count($array).
source
share