Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php in#u array()不';使用关联数组时无法正常工作_Php_Arrays - Fatal编程技术网

Php in#u array()不';使用关联数组时无法正常工作

Php in#u array()不';使用关联数组时无法正常工作,php,arrays,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

我不知道是什么导致了这个问题,但我将在下面发布代码,然后回顾我到目前为止所做的工作和得到的结果

$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.";
}
上面的代码是我一直在玩的,它不工作。但是,如果我们将
的“第一个数组条目”(注释)
的值从
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'
不能为
NULL
FALSE
,唯一有效的值是
TRUE
。如果
$form_email
的值等于一个没有值的键,则该值可以为空;如果该键有值,并且第一个数组键没有
TRUE
值,则整个操作将以任何方式失败

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

  • 有没有关于如何解决这个问题的建议
  • 如果你能帮我理解为什么会这样,也许我做错了什么
  • 编辑:

    我还尝试了以下方法:

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

    您的问题在于您的if声明:

    if (in_array($form_email, $client_emails))
    
    在这里,您可以在值(
    [NULL,“page\u two”,“page\u two”,NULL)]
    中搜索电子邮件,但您需要查看键(
    [”email@ex-one.com“,…,”email@ex-four.com“]
    ),所以只需使用
    array\u keys()
    ,例如

    if (in_array($form_email, array_keys($client_emails)))
                            //^^^^^^^^^^^ See here, so you serach the email in the keys
    

    您正在将
    $form\u email
    $client\u email
    的值进行比较

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

    if (in_array($form_email, array_keys($client_emails))) {
    
    或检查是否存在以下情况:

    if(array_key_exists($form_email, $client_emails)) {
    
    你为什么不使用


    使用!empty()方法检查0,null值。数组键存在,而不是数组中的键应该起作用。我在数组中检查值,但您使用的是键。
    isset
    检查给定变量是否存在且是否为
    null
    @deceze,但由于他随后进行检查,如果电子邮件在那里,它就会起作用,因为如果存在并且isset()返回FALSE,则值为null。请参阅,我感谢您提供的所有详细信息,我知道这对你来说似乎是件简单的事情,但我对PHP还是新手,只做了几个月,我整天都在试图弄清楚这个问题,哈哈,只是等着接受答案@马修,不客气。(因为您刚接触PHP,所以更重要的是,您可以看到哪里出了问题,并了解发生了什么。)
    在数组中(数组键)
    效率非常低。您只需要
    数组\u键\u存在
    @deceze是的,这也是一个选项,但我想从OP的代码中将
    保留在_array()
    中,并显示他在值中搜索。因此,对于数组中的逻辑
    ,我选择使用数组
    \u keys()
    来显示他做错了什么。(我应该包括
    array\u key\u exists()
    作为更好的解决方案,在解释之后,他做错了什么)这确实更有意义!
    if(array_key_exists($form_email, $client_emails)){
    
    }