I have array like below in my session
Array (
[product-2] => Array (
[product_name] => Hello
[pid] => 2
[product_price] => 300
[quantity] => 1
[product_image] => 1630037523.png
)
)
I am looking for change value for field called quantity.
I am searching like below
if (isset($_POST['action']) && $_POST['action']=="change"){
foreach($_SESSION["shopping_cart"] as $key => $value) {
if('product-'.$_POST["pid"] == $key){
echo "found";
$new_quantity = $_POST['quantity'];
//now do not know how to update it in array
break;
}
}
}
I am new in PHP and does not know how to update in my array. Let me know if anyone here can help me for same. Thanks!
$_SESSION["shopping_cart"][$key]['quantity'] = $new_quantity;. Btw, is there a reason why you're prefixing the key withproduct-instead of just using the id straight off? Just seems like extra fluff for no gain.