note 98373 added to function.is-array
| From: | grigory at osu1 dot php dot net | Date: | Fri, 11 Jun 2010 08:30:27 +0000 |
| Subject: | note 98373 added to function.is-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170050@lists.php.net to get a copy of this message | ||
Another version of function is_assoc_array(). It is not short, but seems to be quite effective.
Main idea: when 'foreach' works on a list - it uses numbers as keys, and that keys are
numbers, start with 0, and in sequenced order. Anything another - and array is not a list.
<?php
function is_assoc_array( $obj )
{
if( ! is_array( $obj ) )
return false;
$islist = true;
foreach( $obj as $key => $val )
{
if( gettype( $key ) == 'string' )
return true;
if( isset( $prev_key ) && $key !== 0 )
{
if( $key != $prev_key + 1 )
return true;
}
$prev_key = $key;
}
return false;
}
?>
----
Server IP: 195.54.192.44
Probable Submitter: 127.0.0.1
----
Manual Page -- http://www.php.net/manual/en/function.is-array.php
Edit -- https://master.php.net/note/edit/98373
Del: integrated -- https://master.php.net/note/delete/98373/integrated
Del: useless -- https://master.php.net/note/delete/98373/useless
Del: bad code -- https://master.php.net/note/delete/98373/bad+code
Del: spam -- https://master.php.net/note/delete/98373/spam
Del: non-english -- https://master.php.net/note/delete/98373/non-english
Del: in docs -- https://master.php.net/note/delete/98373/in+docs
Del: other reasons-- https://master.php.net/note/delete/98373
Reject -- https://master.php.net/note/reject/98373
Search -- https://master.php.net/manage/user-notes.php