Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

skaimauve at yahoo dot ca
16 years ago
Or you could make use of the array_diff_key and array_key function:

<?php

function is_assoc($var)
{
return
is_array($var) && array_diff_key($var,array_keys(array_keys($var)));
}

function
test($var)
{
echo
is_assoc($var) ? "I'm an assoc array.\n" : "I'm not an assoc array.\n";
}

// an assoc array
$a = array("a"=>"aaa","b"=>1,"c"=>true);
test($a);

// an array
$b = array_values($a);
test($b);

// an object
$c = (object)$a;
test($c);

// other types
test($a->a);
test($a->b);
test($a->c);

?>

The above code outputs:
I'm an assoc array.
I'm not an assoc array.
I'm not an assoc array.
I'm not an assoc array.
I'm not an assoc array.
I'm not an assoc array.

<< Back to user notes page

To Top