欢迎光临
我们一直在努力

PHP数组函数key_exists()的用法

PHP key_exists( ) 函数用于检查给定的键或索引是否存在于数组中。它是 array_key_exists( ) 的别名。该函数是在 PHP 4.0.6 中引入的。

用法

bool key_exists ( mixed $key , array $array );

参数

参数 描述 是强制性的
array 指定要使用的数组。 compulsory
key 指定 key 。 compulsory

返回

如果键存在,该函数返回真,如果键不存在,则返回假。

例子1

<?php
$subjects = array("sonoo"=>"OS","ajeet"=>"compiler","jin"=>"dbms");
if (key_exists("sonoo", $subjects)) 
{
		echo "Key exists!";
}
else
{
		echo "Key does not exist!";
}
?>

输出:

Key exists!

例子2

<?php
$car=array("tata"=>"jaguar","BMW"=>"X6");
if (array_key_exists("tata",$car))
	{
		echo "Key exists!";
	}
else
	{
		echo "Key does not exist!";
	}
?>

输出:

Key exists!

例子3

<?php
$array = array("ram"=>25, "krishna"=>10, "aakash"=>20, "gaurav");
if (key_exists("krishna", $array)) 
{
echo "Key exists!";
}
else
{
		echo "Key does not exist!";
}
?>

输出:

Key exists!

示例 4

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

输出:

Key exists!
赞(0) 打赏
未经允许不得转载:码农资源网 » PHP数组函数key_exists()的用法
分享到

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册