0

I am creating A shooping cart using php i am updating my quantity on one update button correctly but when i delete a product that is set on an array index my index in not reindexing because i cant access my array

    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");  

    }
    }
}
Talha
  • 522
  • 1
  • 6
  • 22

1 Answers1

0

Sounds like you need to reindex your array after deleting a product.

Read this:
How do you reindex an array in PHP?

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

user3720435
  • 1,343
  • 1
  • 14
  • 25