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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度