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


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

,但我不知道如何使这个编码正确。我还在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>";

您可以将价格指定为数组的值,将名称指定为数组的键,因此:

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