Home  >  Article  >  Backend Development  >  PHP determines whether a specified key exists in an array

PHP determines whether a specified key exists in an array

王林
王林forward
2024-03-21 21:21:06522browse

Determining whether a specified key exists in an array in PHP is one of the common problems in programming. In PHP, we can use the array_key_exists() function to determine whether the specified key exists in the array. If it exists, it returns true, otherwise it returns false. Through this simple method, we can easily determine whether the array contains the key we need, and perform corresponding logical processing. Let's take a look at the specific implementation method.

PHP determines whether the specified key exists in an array

In php, there are many ways to determine whether a specified key exists in an array:

1. Use isset() function:

isset($array["key"])

This function returns a Boolean value. If the specified key exists, it returns true, otherwise it returns false.

2. Use array_key_exists() function:

array_key_exists("key", $array)

This function also returns a Boolean value, but unlike isset(), it does not detect inherited properties.

3. Use in_array() function (not recommended):

in_array("key", array_keys($array))

This function returns a Boolean value, but it converts the array's key to a string, so numeric keys may not be detected.

4. Use array_search() function:

array_search("key", $array)

This function returns the index of the specified key in the array. If the key does not exist, it returns false.

5. Use = operator:

$array["key"] = 0;

If the key exists, this operation will increase the value corresponding to the key by 0, otherwise a new key will be added and its value will be set to 0. Then check whether $array["key"] is 0 to determine whether the key exists.

6. Use empty() function (not recommended):

empty($array["key"])

This function returns a Boolean value. If the key does not exist or is empty, it returns true, otherwise it returns false.

Performance comparison:

  • isset() and array_key_exists() perform best for large arrays.
  • in_array() and array_search() have better performance for small arrays.
  • = operator and empty() have the worst performance and are not recommended.

Choose the best method:

Which method to choose depends on the specific situation:

  • If you need to distinguish between the key not existing and null, use isset().
  • If inherited properties are not considered, use array_key_exists().
  • If you want to get the index of the key, use array_search().
  • If you need to add a new key or perform mathematical operations on the value of an existing key, use the = operator.

Example:

$array = ["name" => "John", "age" => 30];

if (isset($array["name"])) {
echo "Name exists";
}

if (array_key_exists("age", $array)) {
echo "Age exists";
}

if (array_search("name", $array) !== false) {
echo "Name has index " . array_search("name", $array);
}

$array["hobby"] = 0;
if ($array["hobby"] === 0) {
echo "Hobby does not exist";
}

The above is the detailed content of PHP determines whether a specified key exists in an array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete