Вы можете получить ключ предмета с помощью array_search
, например:
// look for 'item_id' with the value of $_GET["product_id"] inside of the cart
$itemIds = array_column( $_SESSION["shopping_cart"], "item_id" );
$key = array_search( $_GET["product_id"], $itemIds );
и затем вы можете легко обновить количество этого элемента, выполнив что-то вроде этого:
...
if (isset($_SESSION["shopping_cart"]))
{
// look for 'item_id' with the value of $_GET["product_id"] inside of the cart
$itemIds = array_column( $_SESSION["shopping_cart"], "item_id" );
$key = array_search( $_GET["product_id"], $itemIds );
if ( $key === false )
{
$item_array = array(
'item_id' => $_GET["product_id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"],
'item_image' => $_POST["hidden_image"],
);
$_SESSION["shopping_cart"][] = $item_array;
}
else
{
// if quantity is invalid do something, else
$_SESSION["shopping_cart"][$key]["item_quantity"] += $_POST["quantity"];
}
}
...