alex frase's example is fast but elanthis at awesomeplay dot com's example is faster and Ilgar's modification of alex's code is faulty (the part " || $_array[$k] !== $v"). Also, Ilgar's suggestion of giving a false return value when the variable isnt an array is not suitable in my opinion and i think checking if the array is empty would also be a suitable check before the rest of the code runs.
So here's the modified (is_vector) version
<?php
function is_vector( &$array ) {
if ( !is_array($array) || empty($array) ) {
return -1;
}
$next = 0;
foreach ( $array as $k => $v ) {
if ( $k !== $next ) return true;
$next++;
}
return false;
}
?>
and the modified (alex's is_assoc) version
<?php
function is_assoc($_array) {
if ( !is_array($_array) || empty($array) ) {
return -1;
}
foreach (array_keys($_array) as $k => $v) {
if ($k !== $v) {
return true;
}
}
return false;
}
?>