foreach ($_SESSION['cart'] as $prodid => $qty => $size ){
这行代码不断引发错误。
Parse error: syntax error, unexpected ',', expecting ')'
in C:\xampp\htdocs\ShoeShop\cartPage.php on line 29
有谁知道如何修复它? 我对数组知之甚少。
答案 0 :(得分:0)
我想你想要这样的东西:
foreach($_SESSION['cart'] as $prodid) {
foreach($prodid as $qty => $size) {
}
}
foreach
loops可以采用foreach(array() as $value) {}
或foreach(array() as $key => $value) {}
格式,目前您有foreach(array() as $key => $value => ???) {}
。这将假设$_SESSION['cart']
的格式为:
Array(
Array(
qty => size,
qty => size,
qty => size,
),
Array(
qty => size,
qty => size,
qty => size,
),
);
如果您的数组看起来更像这样:
Array(
Array(
prodid,
qty,
size,
),
Array(
prodid,
qty,
size,
),
)
然后你的循环会更简单:
foreach($_SESSION['cart'] as $item) {
$prodid = $item['prodid'];
$qty = $item['qty'];
$size = $item['size'];
}