当前位置: 首页> 函数类别大全> key_exists

key_exists

array_key_exists的别名
名称:key_exists
分类:数组
所属语言:php
一句话介绍:array_key_exists的别名

key_exists 函数

适用 PHP 版本

PHP 4 及以上版本(但从 PHP 7.4.0 起建议使用 array_key_exists 函数代替)

函数说明

key_exists() 函数用于检查数组中是否存在指定的键。它的功能与 array_key_exists() 类似,但 array_key_exists() 是更推荐使用的函数。

函数语法

bool key_exists ( mixed $key , array $array )

参数

  • $key:要检查的键名,可以是字符串或整数。
  • $array:要检查的数组。

返回值

如果指定的键名存在于数组中,则返回 TRUE,否则返回 FALSE

示例

判断数组中是否存在键 "username"。

示例代码

<?php
$userInfo = array("username" => "alice", "email" => "[email protected]");
if (key_exists("username", $userInfo)) {
  echo "键 username 存在,值为: " . $userInfo["username"];
} else {
  echo "键 username 不存在";
}
?>

示例代码的说明

该代码定义了一个包含用户信息的数组 $userInfo,使用 key_exists() 函数检查数组中是否存在键 "username"。如果存在,则输出该键对应的值;否则输出键不存在的提示。

同类函数
  • 用“自然排序”算法对数组进行不区分大小写字母的排序 natcasesort

    natcasesort

    用“自然排序”算法对数组进行不区分大小写
  • 用给定的值填充数组 array_fill

    array_fill

    用给定的值填充数组
  • 对数组进行排序并保持索引关系 asort

    asort

    对数组进行排序并保持索引关系
  • 递归地合并一个或多个数组 array_merge_recursive

    array_merge_recursive

    递归地合并一个或多个数组
  • 从关联数组中取得键名 key

    key

    从关联数组中取得键名
  • 对数组中的每个成员递归地应用用户函数 array_walk_recursive

    array_walk_recursive

    对数组中的每个成员递归地应用用户函数
  • 对数组进行逆向排序并保持索引关系 arsort

    arsort

    对数组进行逆向排序并保持索引关系
  • 从数组中取出一段 array_slice

    array_slice

    从数组中取出一段