search for in the  
<intvalis_bool>
Last updated: Thu, 19 May 2005

is_array

(PHP 3, PHP 4, PHP 5)

is_array -- Finds whether a variable is an array

Description

bool is_array ( mixed var )

Finds whether the given variable is an array.

Parameters

var

The variable being evaluated.

Return Values

Returns TRUE if var is an array, FALSE otherwise.

Examples

Example 1. Check that variable is an array

<?php
$yes
= array('this', 'is', 'an array');

echo
is_array($yes) ? 'Array' : 'not an Array';
echo
"\n";

$no = 'this is a string';

echo
is_array($no) ? 'Array' : 'not an Array';
?>

The above example will output:

Array
not an Array



User Contributed Notes
is_array
yonman at nsa dot co dot il
04-Apr-2005 03:31
To expand a bit on dan at cain dot sh's comment - is_array will not properly identify Iterator implementing classes.
dan at cain dot sh
10-Dec-2004 07:05
is_array() under PHP 5.0.2 will return FALSE when passed an object descended from the internal class interface ArrayAccess(http://www.php.net/spl) even though said object behaves as an array would in most instances.

I've found the following user function helpful with my own classes and functions that expect array(s) as arguments, but work fine with objects that behave as an array would.

<?php
function is_array_abled(&$x)
{
   return (bool)(
$x instanceof ArrayAccess or is_array($x));
}
?>
vhermecz at ixpert dot hu
01-Apr-2004 06:58
Mike's function is quite cool, it is just the one, I was searching for. Using range is a great idea! But it's a bit long for me. Here is a shorter version:

function is_assoc_array($var) {
   if (!is_array($var)) {
       return false;
   }
   return array_keys($var)!==range(0,sizeof($var)-1);
}

Or, if you don't want to type that much:

function is_assoc($var) {
   return is_array($var) && array_keys($var)!==range(0,sizeof($var)-1);
}
mike-php at spam dot emerge2 dot spam dot com
22-Aug-2003 05:20
All arrays in PHP are associative arrays, but it is quite easy to treat an associative array just like it is a sequential array. However, when dealing with XML-RPC, it is necessary to know whether an array is associative or sequential, so I created this function.

It isn't perfect, since an associative array that just happens to have sequential, integer keys starting with 0 will 'look' exactly like a sequential array, and will fool this function.

/****************************************************************
* is_assoc_array tries to decide whether or not a given array  *
* is an associative array, or a sequential array. Of course, no *
* such distinction is made by PHP, so it really just tests      *
* whether or not a given array could possibly be a sequential  *
* array. Since an associative array with sequential, integer    *
* keys 'looks' just like a sequential array, this function will *
* be fooled.                                                    *
*                                                              *
* BUG: Associative arrays with sequential, integer keys 'look'  *
* just like sequential arrays, and will be identified as such.  *
*                                                              *
****************************************************************/
function is_assoc_array( $php_val ) {
   if( !is_array( $php_val ) ){
     # Neither an associative, nor non-associative array.
     return false;
   }

   $given_keys = array_keys( $php_val );
   $non_assoc_keys = range( 0, count( $php_val ) );

   if( function_exists( 'array_diff_assoc' ) ) { # PHP > 4.3.0
     if( array_diff_assoc( $given_keys, $non_assoc_keys ) ){
         return true;
     }
     else {
         return false;
     }
   }
   else {
     if( array_diff( $given_keys, $non_assoc_keys ) and array_diff( $non_assoc_keys, $given_keys ) ){
         return true;
     }
     else {
         return false;
     }
   }
}

<intvalis_bool>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT