Home >Backend Development >PHP Tutorial >PHP determines whether it is an array program code_PHP tutorial

PHP determines whether it is an array program code_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:59:01828browse

It is very simple to determine whether a variable is an array in PHP. Using the is_array() function in PHP, you can quickly determine whether a variable is an array. Let me introduce it to you below. ​

is_array — Check if a variable is an array

Report a bug description
bool is_array ( mixed $var )

Returns TRUE if var is an array, otherwise returns FALSE.

Example

The code is as follows Copy code
 代码如下 复制代码

$a =1;

if( is_array( $a ) )
{
  echo '是数组';
}
else
{
   echo '不是数组0';
}

$a =1;

if( is_array( $a ) )
 代码如下 复制代码

function is_vector( &$array ) {
   if ( !is_array($array) || empty($array) ) {
      return -1;
   }
   $next = 0;
   foreach ( $array as $k => $v ) {
      if ( $k !== $next ) return false;
      $next++;
   }
   return true;
}
?>

{

echo 'is an array';

}

else

{
 代码如下 复制代码

function is_assoc($array) {
        if(is_array($array)) {
            $keys = array_keys($array);
            return $keys != array_keys($keys);
        }
        return false;
}

echo 'Not array 0';
}

Example

The code is as follows Copy code
if ( !is_array($array) || empty($array) ) { Return -1; } $next = 0; foreach ( $array as $k => $v ) { If ( $k !== $next ) return false; $next++; } Return true; } ?>
Method to determine whether to index an array
echo is_assoc($array)?'index array':'not an index array';
The code is as follows Copy code
function is_assoc($array) { If(is_array($array)) {                $keys = array_keys($array);                 return $keys != array_keys($keys); }          return false; }
http://www.bkjia.com/PHPjc/445635.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445635.htmlTechArticleIt is very simple to determine whether a variable is an array in PHP. Using the is_array() function in PHP, you can quickly determine whether a variable is an array. Array, let me introduce it to you below. is_array checks whether the variable is a number...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn