php - 给出警告 : in_array() expects parameter 2 to be array, null

标签 php arrays session

尝试将数据添加到 session (并检查它是否已存在)时,我收到以下警告。

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



我怎样才能解决这个问题?

它所指的代码:
if(isset($_GET['product']) && !in_array($_GET['product'], $_SESSION['product'])){
    $_SESSION['product'][] = $_GET['product'];
}

我只有在干净的浏览器上添加第一个产品时才会收到此警告。当我删除它并添加另一个产品时,警告消失了。如果我添加第二个产品也一样。

最佳答案

警告说明了一切。此参数为空:

 $_SESSION['product']

使用前请确保已设置好。例子:
if(isset($_SESSION['product']) && isset($_GET['product']) &&  !in_array($_GET['product'], $_SESSION['product'])){
        $_SESSION['product'][] = $_GET['product'];
    }

关于php - 给出警告 : in_array() expects parameter 2 to be array, null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41565184/

相关文章:

c# - C# 中的数组搜索

hibernate - 我们可以限制从 SessionFactory 获取的 session 数量吗?

session - tomcat如何保持 session

php - MySQL 触发器还是 PHP 进行数据审计?

php - 从当前时间戳默认获取日期时间

javascript - 使用 splice 方法进行数组 chop

php - 保存图像仅在登录时可用

php - PostgreSql 的 CodeIgniter2 + Sparks + Php-ActiveRecord 问题

php - 可测试代码 - 小应用示例

php - 在 PHP 中更新 JSON 数组