考虑这两个例子......
$key = 'jim';
// example 1
if (isset($array[$key])) {
// ...
}
// example 2
if (array_key_exists($key, $array)) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
我很想知道其中任何一个是否更好.我一直使用第一个,但看到很多人在这个网站上使用第二个例子.
那么哪个更好?快点?更明确的意图?
我有各种各样的数组,将包含
story & message
Run Code Online (Sandbox Code Playgroud)
or just
story
Run Code Online (Sandbox Code Playgroud)
How would I check to see if an array contains both story and message? array_key_exists() only looks for that single key in the array.
Is there a way to do this?
我目前正在使用以下内容:
$a = array('foo' => 'bar', 'bar' => 'foo');
if(isset($a['foo']) && isset($a['bar'])){
echo 'all exist';
}
Run Code Online (Sandbox Code Playgroud)
然而,我将有以上几种数组键foo和bar我必须检查.检查每个必需的密钥是否比isset为每个必需的条目添加更有效的方法?