note 73505 added to function.is-array
| From: | angelo[at]mandato" com | Date: | Mon, 26 Feb 2007 21:32:39 +0000 |
| Subject: | note 73505 added to function.is-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123908@lists.php.net to get a copy of this message | ||
The is_associative_array() and is_sequential_array() functions posted by 'rjg4013 at rit dot
edu' are not accurate.
The functions fail to recognize indexes that are not in sequence or in order. For example,
array(0=>'a', 2=>'b', 1=>'c') and array(0=>'a',
3=>'b', 5=>'c') would be considered as sequential arrays. A true
sequential array would be in consecutive order with no gaps in the indices.
The following solution utilizes the array_merge properties. If only one array is given and the array
is numerically indexed, the keys get re-indexed in a continuous way. The result must match the
array passed to it in order to truly be a numerically indexed (sequential) array. Otherwise it can
be assumed to be an associative array (something unobtainable in languages such as C).
The following functions will work for PHP >= 4.
<?php
function is_sequential_array($var)
{
return (array_merge($var) === $var && is_numeric( implode( array_keys( $var ) ) ) );
}
function is_assoc_array($var)
{
return (array_merge($var) !== $var || !is_numeric( implode( array_keys( $var ) ) ) );
}
?>
If you are not concerned about the actual order of the indices, you can change the comparison to ==
and != respectively.
----
Server IP: 64.71.164.2
Probable Submitter: 64.179.48.194
----
Manual Page -- http://www.php.net/manual/en/function.is-array.php
Edit -- https://master.php.net/note/edit/73505
Del: integrated -- https://master.php.net/note/delete/73505/integrated
Del: useless -- https://master.php.net/note/delete/73505/useless
Del: bad code -- https://master.php.net/note/delete/73505/bad+code
Del: spam -- https://master.php.net/note/delete/73505/spam
Del: non-english -- https://master.php.net/note/delete/73505/non-english
Del: in docs -- https://master.php.net/note/delete/73505/in+docs
Del: other reasons-- https://master.php.net/note/delete/73505
Reject -- https://master.php.net/note/reject/73505
Search -- https://master.php.net/manage/user-notes.php