← Back to PHP Course | Chapter 7: Arrays | Lesson 4 of 13

PHP Update Array Items

An array's values are rarely fixed forever -- a shopping cart quantity changes, a user updates their profile, a score increases. Updating an array item means assigning a new value to an existing position or key, overwriting whatever was stored there before, using the same square bracket syntax you use to read it.

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
<?php
$inventory = ["apples" => 20];
$inventory["apples"] = 50;
echo $inventory["apples"];
?>

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
<?php
$prices = [100, 200, 300];
foreach ($prices as &$price) {
    $price *= 0.9;
}
unset($price);
print_r($prices);
?>

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
<?php
$data = ["user" => ["address" => ["city" => "Boston"]]];
$data["user"]["address"]["city"] = "Austin";
echo $data["user"]["address"]["city"];
?>

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
<?php
$original = [1, 2, 3];
$copy = $original;
$copy[0] = 99;
print_r($original);
print_r($copy);
?>

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
<?php
$prices = [5, 15, 8, 20];
foreach ($prices as &$price) {
    if ($price < 10) {
        $price = 10;
    }
}
unset($price);
print_r($prices);
?>
Common Mistakes
  1. 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.
  2. 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.
  3. Looping over an array with foreach and expecting changes made to the loop variable to update the original array, without using a reference (&).
Chapter Summary
  • 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.
Browser Support

Array item assignment has worked identically in every PHP version since arrays were introduced.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.