note 75351 deleted from function.is-array by danbrown
| From: | danbrown@php.net | Date: | Sat, 27 Dec 2008 16:48:10 +0000 |
| Subject: | note 75351 deleted from function.is-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148561@lists.php.net to get a copy of this message | ||
Note Submitter: elanthis at awesomeplay dot com
----
First, "sequential array" is a silly term. Use "vector." Likewise, it's
not an "associative array," it's a "map." We have common computer science
terms for a reason. :)
"If the keys were all numbers then we'd call it "sequential"."
But that's not true. The following array, with all-numeric keys, is a map, not a vector:
$ar[1] = 'foo';
$ar[12] = 'bar';
$ar[578] = 'baz';
A vector would be an array in which all keys are integers, each successive key is equal to the
previous key plus one, and the first key is 0.
So, the only proper, always correct, fail-proof way I can think of to do this is (and this is lame
we have to do ths, since the internal PHP implementation clearly knows in a much more efficient
manner):
function is_vector (&$array) {
$next = 0;
foreach ($array as $k=>$v) {
if ($k != $next)
return false;
$next++;
}
return true;
}
Yes, it might look cleaner to use array_keys() in there, but I think that will be far more
inefficient, since it would have to create a whole new array and copy all the keys into it. The
above should be faster. In theory.