in_array()没有按关联数组的预期工作

时间:2015-05-25 08:53:01

标签: php arrays

我不知道造成这个问题的原因,但我会在下面发布代码,然后查看我到目前为止所做的工作以及我得到的结果

$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

'first array entry'不能是NULLFALSE,唯一有效的值是TRUE。如果$form_email的值等于没有值的键,如果键具有值并且第一个数组键没有TRUE的值,那么它可以为空事情无论如何都会失败。

Code to reproduce the issue

我不明白发生了什么。我有两个问题:

  1. 有关如何解决此问题的任何建议?
  2. 如果你能帮助我理解为什么会这样 - 也许我做错了什么?
  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 (!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.";
    }
    

3 个答案:

答案 0 :(得分:2)

你的问题是你的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

答案 1 :(得分:2)

为什么不使用array_key_exists

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

}

答案 2 :(得分:0)

您正在将 <site name="MYWEBSITE" id="4"> <application path="/" applicationPool="Clr4IntegratedAppPool"> <virtualDirectory path="/" physicalPath="D:\MYWEBSITE" /> </application> <bindings> <binding protocol="https" bindingInformation="*:44305:localhost" /> <binding protocol="http" bindingInformation="*:6689:localhost" /> </bindings> </site> $form_email的值进行比较。

$client_emails

if (in_array($form_email, $client_emails)) { 应与$form_email的键进行比较,而不是值。 - 尝试 -

$client_emails

或检查它们是否存在 -

if (in_array($form_email, array_keys($client_emails))) {
相关问题