


In PHP development, we often need to query whether a specific key name exists from an array. For example, we need to determine whether a certain configuration option exists, or whether a user has specific permissions. At this time, you need to use some array functions of PHP to query whether a certain key name exists.
Below, I will introduce several commonly used methods to query whether a certain key name exists in an array.
Method 1: array_key_exists()
array_key_exists() function is a built-in function in PHP, used to determine whether the specified key name exists in an array. The syntax of this function is as follows:
bool array_key_exists (mixed $key, array $array)
Among them, $key represents the key name to be queried, and $array is the array to be queried. This function returns a Boolean value indicating whether the key exists in the array.
The following is a sample code:
$config = array( 'host' => 'localhost', 'port' => '3306', 'username' => 'root', 'password' => '123456' ); if (array_key_exists('host', $config)) { echo 'host exists'; } else { echo 'host does not exist'; }
The output result is: host exists
Method 2: isset()
isset() function is PHP built-in Another function that checks whether a variable has been set and is non-null. In an array, this function can be used to check whether a key exists. The syntax of this function is as follows:
bool isset (mixed $var [, mixed $var2 [, ...]] )
Among them, $var can be a variable or an array. $var2, $var3, etc. are optional parameters used to check more key names. This function returns a Boolean value indicating whether the key name to be checked exists.
The following is a sample code:
$config = array( 'host' => 'localhost', 'port' => '3306', 'username' => 'root', 'password' => '123456' ); if (isset($config['host'])) { echo 'host exists'; } else { echo 'host does not exist'; }
The output result is: host exists
Method three: in_array()
in_array() function is used in Searches an array for a specified value. The syntax of this function is as follows:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Among them, $needle represents the value to be searched, and $haystack is The array to search, $strict indicates whether strict mode is turned on. This function returns a Boolean value indicating whether the value being searched exists in the array.
We can query whether the key name exists by comparing the key name to be searched with the key name in the array. The following is a sample code:
$config = array( 'host' => 'localhost', 'port' => '3306', 'username' => 'root', 'password' => '123456' ); if (in_array('host', array_keys($config))) { echo 'host exists'; } else { echo 'host does not exist'; }
The output result is: host exists
Method 4: array_search()
array_search() function is used to search for the specified value in the array and returns its key name. The syntax of this function is as follows:
mixed array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Among them, $needle represents the value to be searched, and $haystack is The array to search, $strict indicates whether strict mode is turned on. This function returns a key name that represents the position in the array of the value to be searched for.
Similar to the in_array() function, we can use the array_search() function to compare the key name to be searched with the key name in the array to query whether the key name exists. The following is a sample code:
$config = array( 'host' => 'localhost', 'port' => '3306', 'username' => 'root', 'password' => '123456' ); if (array_search('host', array_keys($config)) !== false) { echo 'host exists'; } else { echo 'host does not exist'; }
The output result is: host exists
Summary
The above are several commonly used methods to query whether a certain key name exists in an array. In actual development, which method to use depends on specific needs and personal habits. You can choose the method that suits you based on your needs.
The above is the detailed content of How to check if there is a certain key name in php (three methods). 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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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

SublimeText3 Linux new version
SublimeText3 Linux latest version
