会话数组问题

时间:2014-04-23 21:35:24

标签: php arrays session loops for-loop

foreach ($_SESSION['cart'] as $prodid => $qty => $size ){

这行代码不断引发错误。

Parse error: syntax error, unexpected ',', expecting  ')' 
in C:\xampp\htdocs\ShoeShop\cartPage.php on line 29

有谁知道如何修复它? 我对数组知之甚少。

1 个答案:

答案 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'];
}