I am trying to validate whether or not an array is listed in an array. I am trying to add a product name and url to an session, the session will contain all the products visited by a visitor, but I don't want it to add the same product twice, hence the validation. So if the product is already in the array, I want it to do nothing, but if it doesn't already belong to the array, it needs to be added. This is as far as I got so far. The only issue seems to be the validation.
$viewed_product_url = $viewed_base.$_SERVER['REQUEST_URI'];
if(!isset($_SESSION['products'])) {
$_SESSION['products'] = array('product'=>$products_name,'url'=>$viewed_product_url);
} else {
$found = false;
foreach($_SESSION['products'] as $each_item) {
while(list($key,$value)=each($each_item)) {
if($key == 'product' && $value == $products_name) {
$found = true;
}
}
}
if($found==false){
echo 'found';
$_SESSION['products'][] = array('product'=>$products_name,'url'=>$viewed_product_url);
}
}
these are the errors I am getting
Warning: Variable passed to each() is not an array or object in C:\xampp\htdocs\customers\msl\product.php on line 10
Warning: Variable passed to each() is not an array or object in C:\xampp\htdocs\customers\msl\product.php on line 10 found
So I just want to know how you can check if an array is already in an multivariate array. Or if there are any other alternatives to achieving what I want here.
$_SESSION['products']as a 2-dimensional array. But when you initialize it, it's just a 1-dimensional associative array.