Home  >  Article  >  Backend Development  >  php check if array subscript exists

php check if array subscript exists

王林
王林Original
2019-09-27 17:51:515941browse

php check if array subscript exists

PHP method to check whether array subscript exists

array_key_exists() function

array_key_exists () function determines whether the specified key exists in an array. If the key exists, it returns true, otherwise it returns false.

Syntax:

array_key_exists(key,array)

Parameter description:

key required. Specifies the key name.

array Required. Specifies the input array.

Instance:

<?php
$a=array("a"=>"Dog","b"=>"Cat");
if (array_key_exists("a",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>

Output:

Key exists!

Instance:

<?php
$a=array("a"=>"Dog","b"=>"Cat");
if (array_key_exists("c",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>

Output:

Key does not exist!

Instance:

<?php
$a=array("Dog",Cat");
if (array_key_exists(0,$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>

Output:

Key exists!

Recommended tutorial: PHP video tutorial

The above is the detailed content of php check if array subscript exists. For more information, please follow other related articles on the PHP Chinese website!

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