简体   繁体   English

警告:in_array()期望参数2为数组,给定null

[英]Warning: in_array() expects parameter 2 to be array, null given

I am getting the following warning when trying to add data to a session (and checking if it already exists). 尝试将数据添加到会话(并检查其是否已存在)时,出现以下警告。

Warning: in_array() expects parameter 2 to be array, null given 警告:in_array()期望参数2为数组,给定null

How can I fix this? 我怎样才能解决这个问题?

The code it is referring to: 它所指的代码:

if(isset($_GET['product']) && !in_array($_GET['product'], $_SESSION['product'])){
    $_SESSION['product'][] = $_GET['product'];
}

I only get this warning when adding the first product on a cleaned browser. 仅当在干净的浏览器中添加第一个产品时,我才收到此警告。 When I remove it and add another product the warning is gone. 当我删除它并添加其他产品时,警告消失了。 Same if I add a second product. 如果添加第二个产品,则相同。

The warining says it all. 警告说明了一切。 This param is null: 此参数为空:

 $_SESSION['product']

Make sure it is set before you use it. 使用前请确保已设置好。 Example: 例:

if(isset($_SESSION['product']) && isset($_GET['product']) &&  !in_array($_GET['product'], $_SESSION['product'])){
        $_SESSION['product'][] = $_GET['product'];
    }

Your $_SESSION['product'] is empty. 您的$_SESSION['product']为空。 Try this, 尝试这个,

if(!empty($_SESSION['product']) && isset($_GET['product']) && !in_array($_GET['product'], $_SESSION['product'])){
    $_SESSION['product'][] = $_GET['product'];
}

It should work. 它应该工作。

check if the value is set before use it with isset and use is_array to check if a given variable is an array. 在将其与isset一起使用之前,请检查该值是否已设置,并使用is_array来检查给定变量是否为数组。

if(isset($_GET['product']) && is_set($_SESSION['product']) && is_array($_SESSION['product']) && !in_array($_GET['product'], $_SESSION['product'])){
    $_SESSION['product'][] = $_GET['product'];
}

你应该总是检查数组

isset( $_SESSION['product']) in your is condition before & condition 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 警告:in_array() 期望参数 2 是数组,null 在 /home/ 中给出 - PHP Warning: in_array() expects parameter 2 to be array, null given in /home/ 警告:in_array()期望参数2为数组,给定null(循环) - Warning: in_array() expects parameter 2 to be array, null given (loop) PHP警告:in_array()期望参数2为数组,给定null - PHP Warning: in_array() expects parameter 2 to be array, null given 警告:in_array()期望参数2为数组,在中给出null - Warning: in_array() expects parameter 2 to be array, null given in in_array()期望参数2为数组,给定null - in_array() expects parameter 2 to be array, null given in_array() 期望参数 2 是数组,空值在 - in_array() expects parameter 2 to be array, null given in 警告:in_array()期望参数2是数组,在第1253行的[...]中给出布尔值 - Warning: in_array() expects parameter 2 to be array, boolean given in […] on line 1253 PHP 警告:in_array() 期望参数 2 是数组,字符串在 - PHP Warning: in_array() expects parameter 2 to be array, string given in 警告:in_array() 期望参数 2 是数组,资源在 - Warning: in_array() expects parameter 2 to be array, resource given in PHP 警告:in_array() 期望参数 2 为数组,null 在 /home/co 中给出 - PHP Warning: in_array() expects parameter 2 to be array, null given in /home/co
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM