Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 649
twan Avatar asked Mar 10 '23 04:03

twan


1 Answers

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'];
    }
like image 186
malutki5200 Avatar answered Mar 24 '23 19:03

malutki5200