我有一个购物车系统,使用array_push
将购物车商品添加到购物车。问题是它不会将项目的价格添加到数组中。我怎样才能添加它:
Item - ItemPrice
Item_2 - ItemPrice_2
Item_3 - ItemPrice_3
作为一组。不作为单独的项目
Item
ItemPrice
Item_2
ItemPrice_2
Item_3
ItemPrice_3
我添加它的代码是:
array_push($_SESSION['cart'],'Item_2');
有没有办法通过此Item_2
添加价格
编辑:或者我应该做
itemName -> item_1, item_2, item_3 itemPrice -> itemPrice_1, itemPrice_2, itemPrice_3
但我不知道如何正确编码。 我也在My Cart页面的表格中呼吁这样:
$array = $_SESSION['cart'];
echo "<table class=cart>";
foreach( $array as $key => $value ){
echo "<tr><td><p>" . $key . "</p></td><td><p>" . $value . "</p></td><td><p><a href=#>Remove?</a></p></td></tr>";
}
echo "</table>";
答案 0 :(得分:2)
您只需将价格指定为值,将名称指定为数组的键,即:
$_SESSION['cart'][item] = item_price;
答案 1 :(得分:0)
怎么样:
var categories = [];