本文实例讲述了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 'One' defined? "
.
array_key_exists
(
"One"
,
$array
).
"\n"
);
print
(
"Is '1' defined? "
.
array_key_exists
(
"1"
,
$array
).
"\n"
);
print
(
"Is 'Two' defined? "
.isset(
$array
[
"Two"
]).
"\n"
);
print
(
"Is '2' defined? "
.isset(
$array
[2]).
"\n"
);
?>
返回结果如下:
Is 'One' defined? 1
Is '1′ defined?
Is 'Two' defined? 1
Is '2′ defined?