How to detect whether array key exists in php

青灯夜游
Release: 2023-03-15 20:34:01
Original
2085 people have browsed it

In PHP, you can use the array_key_exists() function to detect whether the specified array key exists. This function accepts two non-omitable parameters, the syntax is "array_key_exists(key,array)", if the key name exists Returns true, or false if the key name does not exist.

How to detect whether array key exists in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use the array_key_exists() function To detect whether the specified array key exists.

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

This function accepts two parameters that cannot be omitted:

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

Example 1:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
{
    echo "指定数组键存在";
}
else
{
    echo "指定数组键不存在";
}
?>
Copy after login

How to detect whether array key exists in php

Please remember that if you omit the key name when specifying the array , will generate integer key names starting from 0 and increasing by 1.

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

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$a=array("Volvo","BMW");
if (array_key_exists(0,$a))
{
    echo "指定数组键存在";
}
else
{
    echo "指定数组键不存在";
}
?>
Copy after login

How to detect whether array key exists in php

Recommended study: "PHP Video Tutorial

The above is the detailed content of How to detect whether array key exists in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!