Home  >  Article  >  Backend Development  >  php判断数组中是否存在指定键(key)的方法_PHP

php判断数组中是否存在指定键(key)的方法_PHP

WBOY
WBOYOriginal
2016-06-01 11:06:151389browse

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

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

array_key_exists语法如下

array_key_exists($key, $array)

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

isset($array[$key])

如果键存在返回true

演示代码如下:

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

返回结果如下:

Is 'One' defined&#63; 1
Is '1′ defined&#63;
Is 'Two' defined&#63; 1
Is '2′ defined&#63;

希望本文所述对大家的php程序设计有所帮助。

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