首页 > 解决方案 > 我被困在 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

解决方案


听起来您需要在删除产品后重新索引您的数组。

阅读本文:
如何在 PHP 中重新索引数组?

这:
https ://www.php.net/manual/en/function.array-values.php


推荐阅读