gpt4 book ai didi

php - array_key_exists 运行不正常

转载 作者:行者123 更新时间:2023-12-05 08:56:58 25 4
gpt4 key购买 nike

我有这个数组:

$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?在第一个数组中,值也为 null,就像第二个数组一样。那么,为什么第一个数组返回 false 而第二个数组返回 true

最佳答案

array_key_exists()搜索键而不是值。

在第一种情况下,x3 是有值(value)的。

所以,它不是搜索。

在这种情况下,您可以使用 in_array() , 这个函数搜索值(value)。

在第二种情况下,x3 是关键,因此可以正确搜索。

关于php - array_key_exists 运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36740082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com