note 89332 added to function.is-array
| From: | skaimauve at yahoo dot ca | Date: | Tue, 03 Mar 2009 20:12:19 +0000 |
| Subject: | note 89332 added to function.is-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151416@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 216.235.13.26
Probable Submitter: 205.237.66.62
----
Manual Page -- http://www.php.net/manual/en/function.is-array.php
Edit -- https://master.php.net/note/edit/89332
Del: integrated -- https://master.php.net/note/delete/89332/integrated
Del: useless -- https://master.php.net/note/delete/89332/useless
Del: bad code -- https://master.php.net/note/delete/89332/bad+code
Del: spam -- https://master.php.net/note/delete/89332/spam
Del: non-english -- https://master.php.net/note/delete/89332/non-english
Del: in docs -- https://master.php.net/note/delete/89332/in+docs
Del: other reasons-- https://master.php.net/note/delete/89332
Reject -- https://master.php.net/note/reject/89332
Search -- https://master.php.net/manage/user-notes.php