Two methods: 1. Use the "array_key_exists("key name", array)" statement. If it is included, it will return true, otherwise it will not be included. 2. Use isset() to detect whether the element corresponding to the specified key name exists. The syntax is "isset($array name["key name"])". If it is included, it returns true, otherwise it is not included.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php detection array Whether to include two methods of specifying keys
1. Use the array_key_exists() function
array_key_exists($key,$array)
The 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.
<?php header("Content-type:text/html;charset=utf-8"); $a=array("Volvo"=>"XC90","BMW"=>"X5"); if (array_key_exists("Toyota",$a)) { echo "指定键存在"; } else { echo "指定键不存在"; } ?>
2. Use the isset() function
isset() function is used to detect whether the variable has been set and is not NULL.
Just use the isset() function to detect whether the specified array element $array["key name"]
exists.
<?php header("Content-type:text/html;charset=utf-8"); $a=array("Volvo"=>"XC90","BMW"=>"X5"); if (isset($a["BMW"])) { echo "指定键存在"; } else { echo "指定键不存在"; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to detect whether an array contains a specified key in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools
