Home  >  Article  >  php教程  >  php中如何判断数组中是否存在指定键(key)

php中如何判断数组中是否存在指定键(key)

WBOY
WBOYOriginal
2016-06-06 20:06:283033browse

这篇文章主要介绍了php判断数组中是否存在指定键(key)的方法,实例分析了php中array_key_exists和isset的使用技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php判断数组中是否存在指定键(key)的方法。分享给大家供大家参考。具体分析如下:

php中有两个函数用来判断数组中是否包含指定的键,分别是array_key_exists和isset

array_key_exists语法如下

array_key_exists($key, $array)

如果键存在返回true isset函数语法如下

isset($array[$key])

如果键存在返回true

演示代码如下:

<?php
$array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");
print("Is &#39;One&#39; defined? ".array_key_exists("One", $array)."\n");
print("Is &#39;1&#39; defined? ".array_key_exists("1", $array)."\n");
print("Is &#39;Two&#39; defined? ".isset($array["Two"])."\n");
print("Is &#39;2&#39; defined? ".isset($array[2])."\n");
?>

返回结果如下:

Is &#39;One&#39; defined? 1
Is &#39;1′ defined?
Is &#39;Two&#39; defined? 1
Is &#39;2′ defined?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn