我有一個會議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