2012-07-03 152 views
0

這讓我難堪。 print_r顯示正確的數組索引和值,但foreach構造檢索錯誤的值,甚至改變最後一個索引的值,即使我沒有通過引用檢索值(不使用&符號)。按下更新按鈕後

<?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'; ?> 

示例輸出是這樣的:

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 
) 

最後指數的值變的變化在每個迭代的當前索引值。這導致最後的值輸出具有與倒數第二個索引相同的值。

幫助?

+0

[循環引用後的奇怪行爲 - 這是一個PHP的bug?](http://stackoverflow.com/questions/8220399/strange-behaviour-after-loop-by-reference-is-this- a-php-bug) – deceze

回答

3

您使用&$value參考前:

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

變量仍然存在,因爲超出了循環引用,在一個循環中已經預期,但非明顯副作用再次使用它。這甚至在一個大紅色框in the manual中提到。 unset($value)之後的第一個循環,以避免這一點。

+0

啊!謝謝。今天學到的另一件新事物。 – DanielP

+0

謝謝!那讓我瘋狂! – Bakaburg

0

您在這裏使用的引用:

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

無論是環後不要立即使用此處引用或取消$值。