Home > Backend Development > PHP Tutorial > php check if array subscript exists

php check if array subscript exists

王林
Release: 2023-04-07 13:44:02
Original
6001 people have browsed it

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)
Copy after login

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!";
}
?>
Copy after login

Output:

Key exists!
Copy after login
Copy after login

Instance:

<?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

Instance:

<?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

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!

Related labels:
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