使用array_key_exists检查键名或索引是否存在于数组

array_key_exists() 在给定的 key 存在于数组中时返回 TRUE。
key 可以是任何能作为数组索引的值。array_key_exists() 也可用于对象。

<?php
$search_array = array(’first’ => 1, ’second’ => 4);
if (array_key_exists(’first’, $search_array)) {
echo “The ‘first’ element is in the array”;
}
?>

array_key_exists() 与 isset() 对比

isset() 对于数组中为 NULL 的值不会返回 TRUE,而 array_key_exists() 会。

<?php
$search_array = array(’first’ => null, ’second’ => 4);
// returns false
isset($search_array['first']);
// returns true
array_key_exists(’first’, $search_array);
?>

相关日志


如果你觉得这篇文章不错,你可以 给我留个回复订阅它。无论如何,谢谢你的支持!

评论

还没有评论。

发表评论

(必填)

(必填)