在关联数组中,in_array()函数不能按预期工作

3

我不知道是什么导致了这个问题,但我会在下面发布代码,然后介绍一下我迄今为止所做过的和我得到的结果。

$client_emails = array(
    'email@ex-one.com' => null, // first array entry
    'email@ex-two.com'   => 'page_two',
    'email@ex-three.com' => 'page_three',
    'email@ex-four.com' => null,
);

$form_email = 'email@ex-two.com';


if (!empty($form_email)) {
    if (isset($client_emails[$form_email])) {
         $client_page = $client_emails[$form_email];
    } else { $client_page = null; }
}

if (in_array($form_email, $client_emails)) {
 if (!is_null($client_page)) {
     echo 'All seems to be good! - ';
     echo $client_page;
 } else {
     echo 'You can not be here.';
 }
} else {
     echo "For some reason this isn't working... 'in_array' should be finding the email in the array.";
}

我一直在试验上面的代码,但它并不起作用。然而,如果我们将'first array entry' (comment)的值从NULL改为TRUE,它就可以工作了,如下所示:

$client_emails = array(
    'email@ex-one.com' => true, // first array entry
    'email@ex-two.com'   => 'page_two',
    'email@ex-three.com' => 'page_three',
    'email@ex-four.com' => null,
);

整个技术方案现在基本上可以工作了,但是TRUE等于1,因此我的脚本的其余部分无法正常工作,因为它会将其读取为1的值并输出。我需要它是NULL
“第一个数组条目”不能为NULLFALSE,唯一可行的值是TRUE。如果$form_email的值等于没有值的键,则可以为空;如果键有值且第一个数组键没有TRUE的值,则整个过程都会失败。 重现问题的代码 我不明白发生了什么。我有两个问题:
  1. 如何解决这个问题?
  2. 如果您能帮助我理解为什么会发生这种情况,也许是我做错了什么?
编辑: 我还尝试了以下内容:
$client_emails = array(
    'email@ex-one.com' => 'null', // first array entry
    'email@ex-two.com'   => 'page_two',
    'email@ex-three.com' => 'page_three',
    'email@ex-four.com' => 'null',
);

$form_email = 'email@ex-two.com';


if (!empty($form_email)) {
    if (isset($client_emails[$form_email])) {
         $client_page = $client_emails[$form_email];
    } else { $client_page = null; }
}

if (in_array($form_email, $client_emails)) {
 if (!empty($client_page) && $client_page != 'null') {
     echo 'All seems to be good! - ';
     echo $client_page;
 } else {
     echo 'You can not be here.';
 }
} else {
     echo "For some reason this isn't working... 'in_array' should be finding the email in the array.";
}

1
数组键存在而不是在数组中应该可以解决问题。我检查数组的值,但你正在使用键。 - scrowler
1
isset 函数用于检查给定的变量是否存在且不为 null - deceze
1
@deceze 但是由于他随后检查电子邮件是否存在,所以它可以工作,因为如果电子邮件存在且isset()返回FALSE,则该值为NULL。 - Rizier123
1
请查看PHP的issetempty权威指南 - deceze
1
@deceze 刚刚读完那篇文章,它帮助我更好地理解了这两个函数,谢谢! - Matthew
显示剩余2条评论
3个回答

3

您的问题在于if语句:

if (in_array($form_email, $client_emails))

在这里,您正在根据值( [NULL,“page_two”,“page_three”,NULL] )搜索电子邮件,但您需要查看键( ["email@ex-one.com",...,“email@ex-four.com"] ),因此只需使用 array_keys(),例如。
if (in_array($form_email, array_keys($client_emails)))
                        //^^^^^^^^^^^ See here, so you serach the email in the keys

我很感激您提供的所有细节,我知道这对您来说可能是一个简单的事情,但我还是PHP的新手,只学了几个月,我一整天都在尝试解决这个问题哈哈哈,现在就等待接受答案! - Matthew
3
@Matthew 没有问题,你刚接触 PHP,更重要的是,你需要能够看到自己犯的错误并理解正在发生的事情。 - Rizier123
in_array(array_keys)非常低效。你只需要使用array_key_exists - deceze
1
@deceze 是的,这也是一个选项,但我想保留 OP 代码中的 in_array() 并展示他在值中搜索的过程。所以对于逻辑上的 in_ array,我选择使用 array _keys() 来展示他的错误之处。(在解释了他的错误之后,我应该包括 array_key_exists() 作为更好的解决方案) - Rizier123

2

为什么不使用array_key_exists呢?

if(array_key_exists($form_email, $client_emails)){

}

那样更有意义! - Matthew

0

您正在将$form_email$client_emails的值进行比较。

if (in_array($form_email, $client_emails)) {

$form_email 应该与 $client_emails 的键进行比较,而不是值。- 请尝试使用 -

if (in_array($form_email, array_keys($client_emails))) {

或者检查它们是否存在 -

if(array_key_exists($form_email, $client_emails)) {

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接