我不知道我的代碼有什麼問題。更新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
我沒有看到你設置$訂購任何東西。我錯過了嗎? – BrettFromLA
建議閱讀以下內容:http://www.php.net/manual/en/language.references.php – ficuscr
Tnx!我會讀這個。 –