Current Location: Home> Function Categories> key_exists

key_exists

alias for array_key_exists
Name:key_exists
Category:Array
Programming Language:php
One-line Description:Check if the specified key name exists in the array

Function name: key_exists()

Applicable version: PHP 4, PHP 5, PHP 7

Usage: The key_exists() function is used to check whether the specified key name exists in the array. Return true if the key name exists; return false if the key name does not exist.

Syntax: bool key_exists ( mixed $key , array $array )

parameter:

  • $key: The key name to check.
  • $array: The array to be checked.

Return value:

  • Return true if the key name exists.
  • If the key name does not exist, false is returned.

Example:

 $fruits = array("apple" => "苹果", "banana" => "香蕉", "orange" => "橙子"); // 检查键名是否存在if (key_exists("apple", $fruits)) { echo "键名'apple' 存在于数组中。"; } else { echo "键名'apple' 不存在于数组中。"; } // 检查键名是否存在if (key_exists("grape", $fruits)) { echo "键名'grape' 存在于数组中。"; } else { echo "键名'grape' 不存在于数组中。"; }

Output:

键名'apple' 存在于数组中。键名'grape' 不存在于数组中。

In the example above, we first create an associative array containing the fruit name. Then, use the key_exists() function to check if the specified key name exists in the array. In the first example, we checked whether the key name 'apple' exists, and because of it, the corresponding message is output. In the second example, we checked whether the key name 'grape' exists, and since it does not exist, the corresponding message is output.

Similar Functions
Popular Articles