【发布时间】:2016-04-20 18:11:59
【问题描述】:
我有这个数组:
$variableNames = [
'x1',
'x2',
'x3',
'x4',
'x5',
'x6',
'x7'
];
但是,当我像这样使用 array_key_exists 函数时:
array_key_exists('x3', $this->variableNames)
它返回false。但是,如果我有这个数组:
$variableNames = [
'x1' => null,
'x2' => null,
'x3' => null,
'x4' => null,
'x5' => null,
'x6' => null,
'x7' => null
];
它返回true。我如何使用第一个数组并获得true?
在第一个数组中,值也为空,就像第二个数组一样。那么,为什么第一个数组返回false,第二个数组返回true?
【问题讨论】:
-
在上面的数组中使用 in_array() 导致键是 0,1,2... 不是 'x1'.....
-
我相信代码在你的第一种情况下认为“键”是值并使键为 0、1、2 等。尝试 var_dump 第一个情况
-
您将键与值混淆了,在您的数组中,键是隐含的 0=>x1 等等
标签: php arrays array-key-exists