我有这样的数组。
Array (
[1] => Array (
[Stock Code] => 1Y 1111
[Price] => 20
[Quantity] => 10
[Amount] => 200
)
[2] => Array (
[Stock Code] => 0300058
[Price] => 30
[Quantity] => 2
[Amount] => 60
)
)
这里是我使用foreach循环从数组中检索值的代码。
<?php
$cartOutput = "";
$i=0;
foreach($_SESSION['cart_array'] as $each_item){
$i++;
$cartOutput = "Stock Code: ".$each_item['Stock Code']."<br/>";
$cartOutput = "Price: ".$each_item['Price']."<br/>";
$cartOutput = "Quantity: ".$each_item['Quantity']."<br/>";
$cartOutput = "Amount: ".$each_item['Amount']."<br/>";
}
?>
这是我在HTML中显示结果的地方
<div style="height:500px;">
<?php echo $cartOutput; ?>
</div>
输出结果为:
金额:60
但我期望结果显示数组的所有值。
答案 0 :(得分:5)
你在每次迭代中覆盖变量。而不是尝试将值附加到变量。试试这样
$cartOutput = "";
$i=0;
foreach($_SESSION['cart_array'] as $each_item){
$i++;
$cartOutput .= "Stock Code: ".$each_item['Stock Code']."<br/>";
$cartOutput .= "Price: ".$each_item['Price']."<br/>";
$cartOutput .= "Quantity: ".$each_item['Quantity']."<br/>";
$cartOutput .= "Amount: ".$each_item['Amount']."<br/>";
}
答案 1 :(得分:1)
在你的foreach循环中你应该有
$cartOutput .=
而不仅仅是=