Itz*_*ham 1 php arrays array-key-exists
我有这个数组:
$variableNames = [
'x1',
'x2',
'x3',
'x4',
'x5',
'x6',
'x7'
];
Run Code Online (Sandbox Code Playgroud)
但是,当我使用这样的array_key_exists函数时:
array_key_exists('x3', $this->variableNames)
Run Code Online (Sandbox Code Playgroud)
它回来了false.但是,如果我有这个数组:
$variableNames = [
'x1' => null,
'x2' => null,
'x3' => null,
'x4' => null,
'x5' => null,
'x6' => null,
'x7' => null
];
Run Code Online (Sandbox Code Playgroud)
它回来了true.我如何使用第一个数组,并得到true?在第一个数组中,值也是null,就像第二个数组一样.那么,为什么第一个数组返回false而第二个数组返回true?
array_key_exists()搜索键而不是值.
在你的第一种情况下,你x3是有价值的.
所以,它没有搜索.
在这种情况下,您可以使用in_array(),此函数搜索值.
在第二种情况下,x3关键,因此,正确搜索.
| 归档时间: |
|
| 查看次数: |
411 次 |
| 最近记录: |