If you want an easy one liner for determining type, you probably don't want to be using arrays at all. You might find that populating your data into an object of a specific class would be more useful.
Then you can test the object type with instanceof
or is_a()
.
Using an object would also give you the ability to provide additional functionality to the data structure. And it could still be treated as an array (eg with foreach()
, etc) by making it an Iterator.
If you prefer to stick with arrays, and you want to distinguish between a keyed and a numeric array, you could check the answers to this question.
The best answer there was a short function along the following lines:
function array_isassoc(array $array) {
return array_keys($array) !== range(0,count($array)-1);
}
This will return true
for a keyed array or false
for a numeric array.
It's probably the most reliable method you could use, although it will stop working if you have a numeric array and remove elements from it without renumbering the keys.