note 62225 added to function.is-array
| From: | mot4h at cs dot virginia dot edu | Date: | Wed, 22 Feb 2006 21:41:16 +0000 |
| Subject: | note 62225 added to function.is-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104245@lists.php.net to get a copy of this message | ||
I was looking at several of the other examples for testing if an array is associative and they
seemed a little limited, or a little bulky. Here's my take on it, I think it's a nice
balance:
<?php
function is_associative($array){
if (!is_array($array)) return false;
foreach(array_keys($array) as $key=>$value) {
if ($key != $value) return true;
}
return false;
}
?>
The idea being that the array_keys() function returns a non-associative array, so if the array you
fed it wasn't associative, all the key/value pairs in the array of keys should be identical.
Otherwise it must have been associative.
----
Server IP: 216.194.113.175
Probable Submitter: 128.143.136.93
----
Manual Page -- http://www.php.net/manual/en/function.is-array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+62225
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62225&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62225&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62225&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62225&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62225&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62225&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62225&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+62225&report=yes
Search -- http://master.php.net/manage/user-notes.php