3

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 protected]' => null, // first array entry
    '[email protected]'   => 'page_two',
    '[email protected]' => 'page_three',
    '[email protected]' => null,
);

$form_email = '[email protected]';


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 protected]' => true, // first array entry
    '[email protected]'   => 'page_two',
    '[email protected]' => 'page_three',
    '[email protected]' => 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.

Code to reproduce the issue

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 protected]' => 'null', // first array entry
    '[email protected]'   => 'page_two',
    '[email protected]' => 'page_three',
    '[email protected]' => 'null',
);

$form_email = '[email protected]';


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.";
}
7
  • 1
    Use !empty() method to check the 0,null values. Commented May 25, 2015 at 8:57
  • 1
    Array key exists instead of in array should do the trick . I'm array checks for values but you're using keys. Commented May 25, 2015 at 8:58
  • 1
    isset checks whether the given variable exists and is not null. Commented May 25, 2015 at 9:02
  • 1
    @deceze But since he then checks, if the e-mail is there, it works, because if it is there and isset() returns FALSE, the value is NULL. Commented May 25, 2015 at 9:04
  • 1
    See The Definitive Guide To PHP's isset And empty Commented May 25, 2015 at 9:06

3 Answers 3

3

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 protected]", ..., "[email protected]"]), 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
Sign up to request clarification or add additional context in comments.

4 Comments

I appreciate all of the detail you've included, I know this may seem like a simple thing on your end but I'm still new to PHP, only been doing this for a few months, I've been trying to figure this out all day lol, just waiting to accept the answer!
@Matthew You're welcome. (Since your new to PHP it's even more important, that you can see, where you went wrong and understand, what's going on.)
in_array(array_keys) is pretty darn inefficient. You just want array_key_exists!
@deceze Yes it is also an option, but I wanted to leave the in_array() from OP's code and show that he searches in the values. So for the logic in_ array I choose to use array _keys() to show what he did wrong. (I should have included array_key_exists() as better solution, after the explanation, what he did wrong)
2

Why don't you use array_key_exists

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

}

1 Comment

That does make more sense!
0

You are comparing $form_email with the values of $client_emails.

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

$form_email should be compared with the keys of $client_emails, not values. - Try with -

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

Or check for the existence of they -

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.