我有一个会议cookie持有一个叫做cart_array的多维数组,我使用一个for each来遍历内部数组,while循环获取键值对。如何检查一个项目是否存在于多维数组中?
我想检查一个项目是否存在于数组中,不仅基于产品id(pid)而且还有其他几个变量,如颜色和大小。这是我到目前为止(但它只检查PID)。我怎样才能检查其他两个变量?
这里是我的变量
$_SESSION['cart_array'] = array(1 => array(
"pid" => $pid,
"quantity" => $quantity,
"color" => $color,
"size" => $size,
"title" => $title,
"product_type" => $product_type,
"price" => $price))
这里是和while循环组合代码:
foreach($_SESSION['cart_array'] as $each_item) {
$index++;
while(list($key, $value) = each($each_item)) {
if($key == "pid" && $value == $pid) {
//That item is in the array
echo "This item is in the array";
} else {
echo "This item is not in the cart";
}
}
}
谢谢大家;从@AgmLauncher得到了解决方案 – andromeda