PHP - 检查数组索引是否存在或为空 [英] PHP - Checking if array index exist or is null
问题描述
有没有办法检查数组索引是否存在或为空?isset()
不会告诉您索引是不存在还是存在但为空.如果我这样做: isset($array[$index]) ||is_null($array[$index])
它不会工作,因为如果索引不存在 is_null 会崩溃.
Is there a way to check if an array index exists or is null?
isset()
doesn't tell you whether the index doesn't exist or exists but is null.
If I do : isset($array[$index]) || is_null($array[$index])
it won't work because if the index doesn't exist is_null will crash.
请问这个怎么查?还有没有办法只检查某个东西是否存在,不管它是否设置为空?
How can I check this please? Also is there a way to check only if something exist, no matter if it is set to null or not?
推荐答案
array_key_exists() 可以做到这一点,property_exists()a> 用于对象,加上 Vineet1982 所说的.感谢您的帮助.
The function array_key_exists() can do that, and property_exists() for objects, plus what Vineet1982 said. Thanks for your help.
这篇关于PHP - 检查数组索引是否存在或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!