note 98373 deleted from function.is-array by danbrown
| From: | danbrown@php.net | Date: | Fri, 11 Jun 2010 14:28:34 +0000 |
| Subject: | note 98373 deleted from function.is-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170060@lists.php.net to get a copy of this message | ||
Note Submitter: grigory
----
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;
}
?>