note 86407 added to function.is-array
| From: | yousuf at philipz dot com | Date: | Thu, 16 Oct 2008 22:41:01 +0000 |
| Subject: | note 86407 added to function.is-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145440@lists.php.net to get a copy of this message | ||
alex frase's example is fast but elanthis at awesomeplay dot com's example is faster and
Ilgar's modification of alex's code is faulty (the part " || $_array[$k] !==
$v"). Also, Ilgar's suggestion of giving a false return value when the variable isnt an
array is not suitable in my opinion and i think checking if the array is empty would also be a
suitable check before the rest of the code runs.
So here's the modified (is_vector) version
function is_assoc( &$array ) {
if ( !is_array($array) || empty($array) ) {
return -1;
}
$next = 0;
foreach ( $array as $k => $v ) {
if ( $k !== $next ) return true;
$next++;
}
return false;
}
and the modified (alex's is_assoc) version
function is_assoc($_array) {
if ( !is_array($_array) || empty($array) ) {
return -1;
}
foreach (array_keys($_array) as $k => $v) {
if ($k !== $v) {
return true;
}
}
return false;
}
----
Server IP: 69.147.83.197
Probable Submitter: 86.96.228.85 (proxied: 92.96.223.116)
----
Manual Page -- http://www.php.net/manual/en/function.is-array.php
Edit -- https://master.php.net/note/edit/86407
Del: integrated -- https://master.php.net/note/delete/86407/integrated
Del: useless -- https://master.php.net/note/delete/86407/useless
Del: bad code -- https://master.php.net/note/delete/86407/bad+code
Del: spam -- https://master.php.net/note/delete/86407/spam
Del: non-english -- https://master.php.net/note/delete/86407/non-english
Del: in docs -- https://master.php.net/note/delete/86407/in+docs
Del: other reasons-- https://master.php.net/note/delete/86407
Reject -- https://master.php.net/note/reject/86407
Search -- https://master.php.net/manage/user-notes.php