note 93433 added to function.is-array
| From: | IpSo at osu1 dot php dot net | Date: | Wed, 09 Sep 2009 23:17:22 +0000 |
| Subject: | note 93433 added to function.is-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160373@lists.php.net to get a copy of this message | ||
This is the fastest way I've come across to determine if an array is associative or purely
numerically keyed. It runs between 2x and 3x faster than any other method I've found in the
comments.
Either way its pretty slow on large arrays, in the 30ms range for 100,000 elements. Other methods
were in the 100-150ms range.
function is_assoc( $array ) {
$i=0;
foreach($array as $k=>$v) {
if ( $k !== $i OR (int)$k !== $k ) {
return TRUE;
}
$i++;
}
return FALSE;
}
----
Server IP: 216.235.13.26
Probable Submitter: 24.71.236.35
----
Manual Page -- http://www.php.net/manual/en/function.is-array.php
Edit -- https://master.php.net/note/edit/93433
Del: integrated -- https://master.php.net/note/delete/93433/integrated
Del: useless -- https://master.php.net/note/delete/93433/useless
Del: bad code -- https://master.php.net/note/delete/93433/bad+code
Del: spam -- https://master.php.net/note/delete/93433/spam
Del: non-english -- https://master.php.net/note/delete/93433/non-english
Del: in docs -- https://master.php.net/note/delete/93433/in+docs
Del: other reasons-- https://master.php.net/note/delete/93433
Reject -- https://master.php.net/note/reject/93433
Search -- https://master.php.net/manage/user-notes.php