Home  >  Article  >  Backend Development  >  How to check if there is a certain key name in php (three methods)

How to check if there is a certain key name in php (three methods)

PHPz
PHPzOriginal
2023-04-04 10:43:331005browse

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!

Statement:
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