PHP array_key_exists() 函数

PHP 教程 · 2019-01-22 09:44:02

实例

检查键名 "Volvo" 是否存在于数组中:

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>

定义和用法

array_key_exists() 函数检查某个数组中是否存在指定的键名,如果键名存在则返回 true,如果键名不存在则返回 false。

提示:请记住,如果您指定数组的时候省略了键名,将会生成从 0 开始并以 1 递增的整数键名。(参阅实例 2)

语法


array_key_exists(key,array)


参数 描述
key 必需。规定键名。
array 必需。规定数组。

技术细节

返回值: 如果键名存在则返回 TRUE,如果键名不存在则返回 FALSE。
PHP 版本: 4.0.7+

更多实例

实例 1

检查键名 "Toyota" 是否存在于数组中:

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (key_exists("Toyota",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>

实例 2

检查整数键名 "0" 是否存在于数组中:

<?php
$a=array("Volvo","BMW");
if (array_key_exists(0,$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>

点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

Learning Processing

Learning Processing

Daniel Shiffman / Morgan Kaufmann / 2008-08-15 / USD 49.95

Book Description Teaches graphic artists the fundamentals of computer programming within a visual playground! Product Description This book introduces programming concepts in the context of c......一起来看看 《Learning Processing》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具