我有一個foreach回圈來檢查所有陣列ID并if使用in_array它來查看是否有任何陣列IDs等于我$_POST['id']的,如下所示:
$cart = array (
'title' => $_POST['title'],
'price' => $_POST['price'],
'img_src' => $_POST['img_src'],
'id' => $_POST['id'],
);
foreach ($_SESSION['cart'] as $item) {
$id = $item['id'];
}
if(in_array($_POST['id'], $id)){
echo "ID exist";
}else{
$_SESSION['cart'][] = $cart;
$count = count($_SESSION["cart"]);
}
出于某種原因,即使ID存在于IDs.
uj5u.com熱心網友回復:
您只是在 foreach 回圈中更改$id的值。嘗試將值存盤在陣列中。參考以下代碼:
$cart = array (
'title' => $_POST['title'],
'price' => $_POST['price'],
'img_src' => $_POST['img_src'],
'id' => $_POST['id'],
);
$id = array();
foreach ($_SESSION['cart'] as $item) {
$id[] = $item['id'];
}
if(in_array($_POST['id'], $id)){
echo "ID exist";
}else{
$_SESSION['cart'][] = $cart;
$count = count($_SESSION["cart"]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/483018.html
上一篇:通過參考傳遞時找到陣列的最小值
下一篇:從二維陣列中洗掉一列
