I have some code that may be passed either an indexed array of values, or an associative array of value pairs. (The parameter is then used to construct radio buttons.) What's the most elegant (least resource hungry?) method for determining which I have?
EDIT: One slight problem with this: what if the array to be tested is as follows....
$d = array(1,2,3,4,4,5);
unset($d[3]);
or even
array_unique($d);
Does this make $d an associative array?
{ I have:
<?php
$a = array(1,2,3,4);
$b = array('one'=>1,"Two"=>2,3=>"Three");
$c = array('one',"Two","Three");
function is_associative($array)
{
$keys = array_keys($array);
foreach ($keys as $key) {
if(!is_int($key)) {
return true;
}
}
return false;
}
var_dump(is_associative($a));
var_dump(is_associative($b));
var_dump(is_associative($c));
?>
which seems cumbersome!}