douyuanqia665858 2012-07-03 10:10
浏览 65
已采纳

Foreach给出了错误的$ value

This has me stumped. print_r displays the correct array indices and values, but the foreach construct retrieves erroneous values and even changes the value for the last index even though I'm not retrieving the values by reference (not using the ampersand).

<?php

require './includes/dbal.php';
require './includes/user.php';
require './includes/book.php';

session_start();

$title='My Shopping Cart';
include './template/header.php';

if(!isset($_SESSION['user']))
{
    die('You are not logged in.');
}

if(!isset($_SESSION['cart']))
{
    $_SESSION['cart'] = array();
}

if(isset($_POST['submit']) && strcmp($_GET['mode'], 'add') == 0)
{
    if(filter_var($_POST['qty'], FILTER_VALIDATE_INT) == FALSE)
    {
        echo '<div style="color: red;">Invalid quantity specified. Please go back and use a valid quantity.</div>';
    }
    else
    {
        $_SESSION['cart'][$_POST['book_id']] = $_POST['qty'];
    }
}

else if(isset($_POST['update']) && strcmp($_GET['mode'], 'update') == 0)
{
    foreach($_SESSION['cart'] as $key => &$value)
    {
        if((int) $_POST["qty_$key"] === 0)
        {
            unset($_SESSION['cart']["$key"]);
        }
        else
        {
            $value = $_POST["qty_$key"];
        }
    }
}

echo '<h3>Your shopping cart</h3>';

$db = new DBal();
$total=0;
echo '<div id="cart-items"><ul><form action="./cart.php?mode=update" method="post">';

// echo 'Original array: '; print_r($_SESSION['cart']);
foreach($_SESSION['cart'] as $key => $value)
{
    // echo '<br />$key => $value for this iteration: ' . "$key => $value<br />";
    // print_r($_SESSION['cart']);
    $b = new Book($key, $db);
    $book = $b->get_book_details();
    $total += $value * $book['book_nprice']
?>
<li>
    <div><img src="./images/books/thumbs/book-<?php echo $book['book_id']; ?>.jpg" title="<?php echo $book['book_name']; ?>" /></div>
    <span class="cart-price">Amount: Rs. <?php echo $value * $book['book_nprice']; ?></span>
    <h3><?php echo $book['book_name']; ?> by <?php echo $book['book_author']; ?></h3>
    Price: Rs. <?php echo $book['book_nprice']; ?><br /><br />
    Qty: <input type="number" name="qty_<?php echo $book['book_id']; ?>" maxlength="3" size="6" min="1" max="100" value="<?php echo $value; ?>" /><br />
</li>
<?php } echo "<span class=\"cart-price\">Total amount: $total</span>" ?>
<br />
<input type="submit" name="update" value="Update Cart" />
</form></ul></div>

<?php include './template/footer.html'; ?>

Sample output after pressing the update button is like this :

Original array:
Array (
    [9] => 6
    [8] => 7
    [3] => 8
)

$key => $value for this iteration: 9 => 6
Array (
    [9] => 6
    [8] => 7
    [3] => 6
)

$key => $value for this iteration: 8 => 7 
Array (
    [9] => 6
    [8] => 7
    [3] => 7
)

$key => $value for this iteration: 3 => 7 
Array (
    [9] => 6
    [8] => 7
    [3] => 7 
)

The value for the last index gets changes to the value of the current index in every iteration. This results in the last value output having the same value as the second-to-last index.

Help?

  • 写回答

2条回答 默认 最新

  • dongwuying0221 2012-07-03 10:13
    关注

    You were using &$value as reference before:

    foreach($_SESSION['cart'] as $key => &$value)
    

    The variable continues to exist as reference beyond the loop, using it again in a loop has expected but non-obvious side effects. This is even mentioned in a big red box in the manual. unset($value) after the first loop to avoid that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路