超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<intvalis_bool>
Last updated: Fri, 22 Jun 2007

is_array

(PHP 3, PHP 4 , PHP 5)

is_array -- 检测变量是否是数组

描述

bool is_array ( mixed var)

如果 vararray,则返回 TRUE,否则返回 FALSE

参见 is_float()is_int()is_integer()is_string()is_object()




add a note add a note User Contributed Notes
is_array
mike-php at spam dot emerge2 dot spam dot com
23-Aug-2003 06: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: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22