林創建的製品陣列,每個具有一個ID和得分:PHP更換數組值
$savedProducts = array('product' => array("product_id" => $product_id,"product_score" => $score));
我希望能夠通過使用作爲PRODUCT_ID標識符更新得分。
我已經試過:
foreach($savedProducts as $key => $val)
{
if ($val == $property_id)
{
$savedProducts[$key] = $score;
break;
}
}
這使添加新的數組元素,而不是更新原來的。
我認爲問題是我的初始數組設置然後與編輯的不匹配。
初始陣列:
Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 4
)
)
[1] => Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 4
)
)
)
)
所以它不斷addding元素,而不是更新現有的:
Array
(
[product] => Array
(
[product_id] => 125026
[product_score] => 5
)
)
試圖更新比分後。
如何'$ savedProperties'樣子:
那麼你就可以訪問你的產品? – fedorqui
有了這個數組結構,你只能有一個'產品',因爲新增加的條目會覆蓋密鑰?!或者「產品」只是其他事物的佔位符? –
你似乎有一個savedProducts和一個savedProperties,我猜他們應該是一樣的。這將使改變你的陣列很容易:-) – vogomatix