需要在数组中的相同值下添加两个变量

Need to Add two varibles under same value in array

我有一个使用 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

但我不知道如何正确编码。 我还在“我的购物车”页面的 table 中这样要求:

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

你可以只指定价格作为值,名称作为数组的键,所以:

$_SESSION['cart'][item] = item_price;

怎么样:

$_SESSION['cart'][] = array('item' => 'itemName', 'price' => 'itemPrice');

foreach ($_SESSION['cart'] as $array) {
    echo "<tr><td>" . $array['item'] . "</td><td>" . $array['price] . "</td><td><a href="#">Remove?</a></td></tr>";
}