想要将键和值附加到已创建的会话。
if (!isset($_SESSION['cart'])) {
$bag = array(
"sessionId" => session_id(),
"productId" => $productId,
"size" => $productSize,
"quantity" => $productQuantity
);
$_SESSION['cart'] = $bag;
} else {
$_SESSION['cart']['sessionId'] = session_id();
$_SESSION['cart']['productId'] = $productId;
$_SESSION['cart']['size'] = $productSize;
$_SESSION['cart']['quantity'] = $productQuantity;
}
如果已创建会话,则将新变量及其键附加到会话中。
$_SESSION['cart']
应该是一个项目数组,而不是你编写的单个项目。每个项目都将是一个单独的关联数组,您可以将其推送到购物车数组上。
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
$bag = array(
"sessionId" => session_id(),
"productId" => $productId,
"size" => $productSize,
"quantity" => $productQuantity
);
$_SESSION['cart'][] = $bag;