PHP function array_key_exists() that checks whether the specified key exists in the array

黄舟
Release: 2023-03-17 08:00:02
Original
1453 people have browsed it

Example

Check whether the key name "Volvo" exists in the array:

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
Copy after login

Definition and usage

array_key_exists() Function Checks whether the specified key name exists in an array. If the key name exists, it returns true. If the key name does not exist, it returns false.

Tip: Please remember that if you omit the key name when specifying the array, an integerkey name starting from 0 and increasing by 1 will be generated. (See Example 2)

Syntax

array_key_exists(key,array)
Copy after login
ParametersDescription
keyRequired. Specifies the key name.
arrayRequired. Specifies an array.

Technical details

Return value: Returns TRUE if the key name exists, if If the key name does not exist, FALSE is returned.
PHP version: 4.0.7+

More examples

Examples 1

Check whether the key name "Toyota" exists in the array:

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (key_exists("Toyota",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
Copy after login

Example 2

Check whether the integer key name "0" exists in the array:

<?php
$a=array("Volvo","BMW");
if (array_key_exists(0,$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
Copy after login

Example 1

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

Output:

Key exists!
Copy after login
Copy after login

Example 2

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

Output:

Key does not exist!
Copy after login

Example 2

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

Output:

Key exists!
Copy after login
Copy after login


The above is the detailed content of PHP function array_key_exists() that checks whether the specified key exists in the array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template