php - 我被困在 php 的情况之间
问题描述
我正在使用 php 创建购物车,我正在一个更新按钮上正确更新我的数量,但是当我删除在数组索引上设置的产品时,我的索引没有重新索引,因为我无法访问我的数组
if(isset($_POST["addcart"])){
$a=productforcart($addcart);
$code=0;
// this $item that i want to access outside
$item = array(
$code=>array(
'pro_id'=>$_GET["id"],
'pro_name'=> $a["productname"],
'pro_details'=> $a["productdetails"],
'pro_price'=> $a["productprice"],
'pro_image'=> $a["productimage"],
'pro_quantity'=>1 )
);
if(empty($_SESSION["shooping_cart"])){
$_SESSION["shooping_cart"]=$item;
header("Location: product.php");
}
else{
$array_keys = array_column($_SESSION["shooping_cart"],"pro_id");
if(in_array($_GET["id"],$array_keys)) {
echo '<script>alert("Product Is Already Added To The Cart")</script>';
echo '<script>window.location="product.php"</script>';
} else {
$_SESSION["shooping_cart"] = array_merge($_SESSION["shooping_cart"],$item);
header("Location: product.php");
}
}
}
解决方案
听起来您需要在删除产品后重新索引您的数组。
阅读本文:
如何在 PHP 中重新索引数组?
推荐阅读
- xamarin.forms - PageRenderer xamarin forms ios中ViewWillAppear中的对象引用崩溃
- android - 在用户通过在主屏幕上向右滑动退出应用程序之前,我们可以在 Android Wear 上添加一个确认屏幕吗?如果可以,如何?
- jooq - Jooq 使用 r2dbc 驱动程序进行响应式获取
- reactjs - screenX 和 screenY 没有让我得到点击的确切坐标
- c# - 从 Unity 向 php 发送变量值
- python - 试图将逗号分隔的文本放入熊猫数据框中
- zoom-sdk - 只需 1 个主持人 ID 同时创建和主持多个 Zoom 会议
- google-maps-urls - 谷歌地图网址,如何将个人姓名添加到目的地?
- javascript - Puppeteer 通过页面重定向获取窗口 URL
- python - SQL Server Native Client 11.0][SQL Server]'»'附近的语法不正确