PHP Update Array Items
In this page:
Updating a Single Item by Key or Index
Assigning a new value to an existing key or index overwrites whatever value was stored there -- $inventory["apples"] = 50 replaces the previous apple count with 50, using the exact same syntax as creating a new key.
Note: Double-check the exact spelling of the key you intend to update; a mismatched key creates a new entry instead of overwriting the intended one.
Warning: PHP does not distinguish between "updating an existing key" and "creating a new one" syntactically -- both use the same assignment, so a typo silently adds an extra key rather than raising an error.
Example: Updating a Single Item by Key or Index
<?php
$inventory = ["apples" => 20];
$inventory["apples"] = 50;
echo $inventory["apples"];
?>
Login to try C/C++/Java/PHP code in the editor
Updating Multiple Items at Once
Looping over an array with foreach and modifying each value by reference (using &$value) lets you update every item in a single pass, applying the same transformation -- like a percentage discount -- to all of them at once.
Note: Always unset() the reference variable right after a foreach loop that uses &$value, to avoid it accidentally overwriting later array items in unrelated code.
Warning: Forgetting the & in foreach ($arr as &$value) means $value is just a copy -- changes to it are lost and never affect the original array.
Example: Updating Multiple Items at Once
<?php
$prices = [100, 200, 300];
foreach ($prices as &$price) {
$price *= 0.9;
}
unset($price);
print_r($prices);
?>
Login to try C/C++/Java/PHP code in the editor
Updating Nested Array Values
Chaining square brackets to the exact nested path lets you update a deeply nested value directly -- $data["user"]["address"]["city"] = "Austin" updates just that one nested field without disturbing anything else in the structure.
Note: When updating deeply nested data, double-check every level of the key path exists first, or the update may create unexpected intermediate structures.
Warning: Updating a nested path where an intermediate level does not yet exist (like $data["user"]["address"] never having been set) will implicitly create it as a new array rather than raising an error.
Example: Updating Nested Array Values
<?php
$data = ["user" => ["address" => ["city" => "Boston"]]];
$data["user"]["address"]["city"] = "Austin";
echo $data["user"]["address"]["city"];
?>
Login to try C/C++/Java/PHP code in the editor
Copy-by-Value Behavior When Updating
When you assign an array to a new variable, PHP copies the entire array by value -- updating the copy leaves the original completely untouched. This is different from objects, which are handled by reference automatically, and often surprises developers coming from other languages.
Note: If you genuinely want two variables to share and update the same array, assign with an explicit reference: $copy = &$original.
Warning: Passing an array into a function without a & in the function's parameter list means the function receives its own copy -- changes inside the function do not affect the caller's original array.
Example: Copy-by-Value Behavior When Updating
<?php
$original = [1, 2, 3];
$copy = $original;
$copy[0] = 99;
print_r($original);
print_r($copy);
?>
Login to try C/C++/Java/PHP code in the editor
Conditionally Updating Array Items
Combining an if condition with array update syntax lets you change a value only when a certain rule is met, like raising every price below a threshold up to a minimum, or marking overdue items differently -- a common pattern when processing a whole array of records.
Note: When conditionally updating items in a loop, keep the condition simple and readable; complex conditional updates are often clearer split into a small named function.
Warning: Conditionally updating items inside a foreach still requires the &$value reference to actually persist changes back to the original array.
Example: Conditionally Updating Array Items
<?php
$prices = [5, 15, 8, 20];
foreach ($prices as &$price) {
if ($price < 10) {
$price = 10;
}
}
unset($price);
print_r($prices);
?>
Login to try C/C++/Java/PHP code in the editor
- Assigning to a brand-new key by accident (a typo in the key name) instead of updating the intended existing one, silently creating an extra, unwanted array entry.
- Updating a copy of an array instead of the original, since PHP arrays are copied by value when assigned to a new variable or passed to most functions.
- Looping over an array with foreach and expecting changes made to the loop variable to update the original array, without using a reference (&).
- Updating an item uses the same square bracket syntax as reading it: $arr["key"] = $newValue.
- PHP arrays are copied by value by default, so updating a copy does not affect the original unless you explicitly use a reference.
- foreach needs an & before its value variable to actually modify the original array's items during the loop.
Array item assignment has worked identically in every PHP version since arrays were introduced.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: