Possible Duplicate:
Why does PHP overwrite values when I iterate through this array twice (by reference, by value)
I got an array which if i print_r it looks like:
Array
(
[0] => Array
(
[0] => 13
[product_id] => 13
[1] => 1
[account_id] => 1
[vat type] => 0
[vat included] => 0
[price] => 3
[unit] => test3
[product number] => 3
)
[1] => Array
(
[0] => 12
[product_id] => 12
[1] => 1
[account_id] => 1
[vat type] => 1
[vat included] => 0
[price] => 2
[unit] => test2
[product number] => 2
)
[2] => Array
(
[0] => 11
[product_id] => 11
[1] => 1
[account_id] => 1
[vat type] => 2
[vat included] => 0
[price] => 1
[unit] => test1
[product number] => 1
)
)
Now when i use a foreach to loop through it something weird happens. when i print_r each subarray in a foreach loop it looks like:
Array
(
[0] => 13
[product_id] => 13
[1] => 1
[account_id] => 1
[vat type] => 0
[vat included] => 0
[price] => 3
[unit] => test3
[product number] => 3
)
Array
(
[0] => 12
[product_id] => 12
[1] => 1
[account_id] => 1
[vat type] => 1
[vat included] => 0
[price] => 2
[unit] => test2
[product number] => 2
)
Array
(
[0] => 12
[product_id] => 12
[1] => 1
[account_id] => 1
[vat type] => 1
[vat included] => 0
[price] => 2
[unit] => test2
[product number] => 2
)
Notice the 3th entry. It's a mysterie to me. Anyone knows why this happens?
the comment of 'deceze' pretty much solved the problem. i used:
foreach($products as &$product){
and than added some entries to $product. replacing that with:
foreach($products as $key=>$product){
and modifying $products[$key] solved the problem
Thanks =D
精彩评论