Check if an array is associative or sequential(indexed) in PHP

In PHP, depending on the type of the keys in an array, the array can be identified as associative (if the key is a string like $arr[‘mykey’]) or sequential (some may call it indexed, i.e. if the key is an integer like $arr[1], $arr[2], etc.).

Although the internal representation of both associative and sequential arrays are the same in the PHP’s core (Both are ordered map), in some situations we still need to differentiate one from another. There is no built-in function to tell if an array is associative or sequential, so we have to write our own.

The most popular method to check this is to compare the keys of an array with the result of range(), like this, this and this:


function isAssoc($arr){
	return array_keys($arr) !== range(0, count($arr) - 1);
}

The problem of using range() is that it only works when the keys of the array are continuous integer numbers. That is, the keys of an array has to be {0,1,2,3,4,…..}. If the array’s smallest key is not 0 but 1, like {1,2,3….}, or there is a “hole” in between, like {1,2,4,5,8,…}. They will be treated as associative array.

Whether this method makes sense depends on how you define the term ”sequential array” and “associative array”. Unfortunately I didn’t find any “official” definition about them in PHP’s manual (as they doesn’t matter to PHP). My personal definition is that when there exist a non-integer key in an array, the array is not sequential. So for the arrays with keys like  {1,2,100, 1000, 1500}, it is still sequential. With this logic, my method to check associative / sequential arrays is this:


function is_asso($a) {
	foreach(array_keys($a) as $key)
		if (!is_int($key)) return TRUE;
	return FALSE;
}


This function will check every keys in the array until it find one that is not an integer, then it quit the loop and tell it is associative array. This function can handle sequential arrays with any starting index, and any hole in between.

3 Replies to “Check if an array is associative or sequential(indexed) in PHP”

  1. While there is no “official” definition of what is a sequential array, I find in my (some)day to day PHP programming activities the need to determine if an array is sequential in functions that need to know if it has to deal with the key or not. Like a general conversion / packing function. In this case, arrays like$a = array();
    $a[] = $value;
    array_push($a, $value2);
    $b = = array('a',2,'c');
    etc...
    will generate a [0], [1], …, [n] indexed array.
    So for mearray_keys($arr) !== range(0, count($arr) - 1);is a good method.

  2. To be sequential, the keys should be in range of 0 ~ (n – 1) where n is the number of items in the array. In often (if not all) times when array is something like (1 => ‘1’, 2 => ‘2’, 100 => ‘3’, 1000 => ‘4’, 1500 => ‘5’), the index keys must be preserved to work properly. One example of this is the curl option array. CURL_* constants are actually integers, and therefore the keys should be intact if you try to store in JSON or something, or otherwise it won’t work.

Leave a Reply

Your email address will not be published. Required fields are marked *