Jak-S
02-19-2006, 03:47 PM
Hi,
Ok, so I know this function doesn’t exist, but is there an equivalent? Something that will return true or false and tell me if an array is associative?
Thanks,
Jack
marek_mar
02-19-2006, 04:12 PM
Associative keys are string keys. This function will check if there is at least one string kay and return true if there is one.
function is_associative($array)
{
if(!is_array($array))
{
return false;
}
krsort($array, SORT_STRING);
return !is_numeric(key($array));
}
Jak-S
02-19-2006, 04:23 PM
Cool, cheers, i had just thrown this together, which works, but i should imagine yours is better as it dosent actually loop through the array, so i guess it would be faster?
function is_associative($input) {
$result = false;
foreach($input as $key => $value) {
if(is_string($key)) {
$result = true;
break;
}
}
return $result;
}
Thanks!