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

查看:118
本文介绍了警告:in_array()期望参数2为数组,给定null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试向会话中添加数据(并检查其是否已存在)时,我收到以下警告.

I am getting the following warning when trying to add data to a session (and checking if it already exists).

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

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'];
}

只有在已清洁的浏览器中添加第一个产品时,我才会收到此警告.当我删除它并添加其他产品时,警告消失了.如果添加第二个产品,则相同.

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'];
    }

这篇关于警告:in_array()期望参数2为数组,给定null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆