PHP Update Array Items
In this page:
$array[index_or_key] = new_value;
$array["outer"]["inner"] = new_value; // nested
foreach ($array as &$value) {
$value = new_value;
}
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.
Example: Updating a Single Item by Key or Index
<?php
// Declare `$inventory` as an array: `["apples" => 20]`
$inventory = ["apples" => 20];
// Set `$inventory["apples"]` to `50`
$inventory["apples"] = 50;
// Print `$inventory["apples"]` to the output
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.
Example: Updating Multiple Items at Once
<?php
// Declare `$prices` as an array: `[100, 200, 300]`
$prices = [100, 200, 300];
// Loop over `$prices`, binding each item to `&$price`
foreach ($prices as &$price) {
$price *= 0.9;
}
// Remove `$price`
unset($price);
// Print a human-readable dump of `$prices`
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.
Example: Updating Nested Array Values
<?php
// Declare `$data` as an array: `["user" => ["address" => ["city" => "Boston"]]]`
$data = ["user" => ["address" => ["city" => "Boston"]]];
$data["user"]["address"]["city"] = "Austin";
// Print `$data["user"]["address"]["city"]` to the output
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.
Example: Copy-by-Value Behavior When Updating
<?php
// Declare `$original` as an array: `[1, 2, 3]`
$original = [1, 2, 3];
// Declare `$copy`, set to `$original`
$copy = $original;
// Set `$copy[0]` to `99`
$copy[0] = 99;
// Print a human-readable dump of `$original`
print_r($original);
// Print a human-readable dump of `$copy`
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.
Example: Conditionally Updating Array Items
<?php
// Declare `$prices` as an array: `[5, 15, 8, 20]`
$prices = [5, 15, 8, 20];
// Loop over `$prices`, binding each item to `&$price`
foreach ($prices as &$price) {
// Check whether `$price < 10`
if ($price < 10) {
// Declare `$price`, set to `10`
$price = 10;
}
}
// Remove `$price`
unset($price);
// Print a human-readable dump of `$prices`
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: