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.
$_SESSION['product']
dosen't contain any data. Try to print$_SESSION['product']
– Nishant Nair Jan 10 '17 at 9:19$_SESSION['product']
is null – Marko Mackic Jan 10 '17 at 9:20$_SESSION["product"] = $_SESSION["product"]?:[]
before the if to initialize the array. Otherwise you'll never add anything to it with this code. – apokryfos Jan 10 '17 at 9:23