2014-01-30 52 views
0

我不知道我的代碼有什麼問題。更新foreach循環中的關聯數組鍵值

我試圖更新qty值,但是當我第二次var_dump數據時,qty不會被更新/重寫。我錯過了什麼?什麼是正確的方法?

$prod = 647bba344396e7c8170902bcf2e15551; 
$qty = 2 
var_dump($ordered); 

array (size=2) 
    '647bba344396e7c8170902bcf2e15551' => 
    array (size=7) 
     'rowid' => string '647bba344396e7c8170902bcf2e15551' (length=32) 
     'id' => string '545' (length=3) 
     'qty' => string '415' (length=3) 
     'price' => string '15.573770' (length=9) 
     'teza' => string '0' (length=1) 
     'name' => string 'Pants 1' (length=14) 
     'subtotal' => float 6463.11455 
    'd64a340bcb633f536d56e51874281454' => 
    array (size=7) 
     'rowid' => string 'd64a340bcb633f536d56e51874281454' (length=32) 
     'id' => string '530' (length=3) 
     'qty' => string '144' (length=3) 
     'price' => string '17.213110' (length=9) 
     'teza' => string '0' (length=1) 
     'name' => string 'Toys' (length=19) 
     'subtotal' => float 2478.68784 

      foreach ($ordered as $item) { 
       if ($item['rowid'] == $prod) 
        $item['qty'] == $qty; 
      } 
     var_dump($ordered);  

array (size=2) 
    '647bba344396e7c8170902bcf2e15551' => 
    array (size=7) 
     'rowid' => string '647bba344396e7c8170902bcf2e15551' (length=32) 
     'id' => string '545' (length=3) 
     'qty' => string '415' (length=3) 
     'price' => string '15.573770' (length=9) 
     'teza' => string '0' (length=1) 
     'name' => string 'Pants white' (length=14) 
     'subtotal' => float 6463.11455 
    'd64a340bcb633f536d56e51874281454' => 
    array (size=7) 
     'rowid' => string 'd64a340bcb633f536d56e51874281454' (length=32) 
     'id' => string '530' (length=3) 
     'qty' => string '144' (length=3) 
     'price' => string '17.213110' (length=9) 
     'teza' => string '0' (length=1) 
     'name' => string 'Pants black' (length=19) 
     'subtotal' => float 2478.68784 
+0

我沒有看到你設置$訂購任何東西。我錯過了嗎? – BrettFromLA

+0

建議閱讀以下內容:http://www.php.net/manual/en/language.references.php – ficuscr

+0

Tnx!我會讀這個。 –

回答

1

試試這個:

 foreach ($ordered as &$item) { // Get array byref 
      if ($item['rowid'] == $prod){ 
       $item['qty'] = $qty; // Fixed evaluation 
      } 
     } 

     var_dump($ordered); 
+0

幹得好,先生! :)這有幫助! –

+0

很好。接受答案並完成! – Springie

+0

約3分鐘。 –

0

== VS =

當你想分配看起來像一個評價。

這是幹什麼的?

$item['qty'] == $qty;

0

你有錯在你的

PHP賦值運算符和比較運算符

= 

爲使用指定的值(賦值運算符)

== 

爲用於合作mpare值

foreach ($ordered as $item) { 
      if ($item['rowid'] == $prod) 
       $item['qty'] = $qty;   

    }