dongzhao5970 2015-05-25 08:53
浏览 83
已采纳

in_array()无法使用关联数组按预期工作

I don't know what's causing this issue, but I'll post the code below then go through what I've done so far and the results I've gotten.

$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.";
}

The above code is what I've been playing with, it does not work. It will work, however, if we change the value for the 'first array entry' (comment) from NULL to TRUE, like so:

$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,
);

The whole thing technically works now, but TRUE is equal to 1 and now the rest of my script does not work properly because it will read that as a value of 1 and echo it. I need it to be NULL.

The 'first array entry' cannot be NULL or FALSE, the only value that works is TRUE. It can be empty IF the value for $form_email is equal to a key that does not have a value, if the key has a value and there is no value of TRUE for the first array key then the whole thing fails any ways.

<kbd>Code to reproduce the issue</kbd>

I don't understand what's happening. I have two questions:

  1. Any suggestions on how to get around this?
  2. If you could help me understand why this is happening -- maybe I'm doing something wrong?

EDIT:

I've also tried the following:

$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条回答 默认 最新

  • dssu33392 2015-05-25 08:57
    关注

    Your problem is your if statement:

    if (in_array($form_email, $client_emails))
    

    Here you search the email in the values ([NULL, "page_two", "page_three", NULL]), but you need to look into the keys (["email@ex-one.com", ..., "email@ex-four.com"]), so just use array_keys(), e.g.

    if (in_array($form_email, array_keys($client_emails)))
                            //^^^^^^^^^^^ See here, so you serach the email in the keys
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?