How to update the value of an array element in PHP
John Mwaniki / Updated on 07 Jul 2024Given an array in PHP and the task to update the value for one or several of its elements, this article explains exactly how to do that.
Arrays in PHP are updatable, you can easily change the value of an array element by assigning it a new value to replace the older one.
This is done in a similar way to updating a variable value, only that in this case you need to specify the index or key of the element you want to update.
Syntax
Array-Variable[index|key] = value
Parameters
Parameter | Description |
---|---|
Array-Variable | This is the name of the variable containing the array to be updated. |
index|key | This specifies the element to be updated in the array. It can be an index or a key depending on the array type. The index is an integer value that identifies an element in an indexed array. A key is a string value that identifies an element in an associative array. |
value | This is the value to be assigned to the array element to replace the old value. It can be an actual or a variable. |
Example 1
Updating the value of an indexed array element.
<?php
$vehicles = array("Tesla", "Toyota", "Audi", "Nissan", "Mazda");
echo "Array before updating an element value: <br>";
print_r($vehicles);
echo "<br>";
$vehicles[3] = "Volvo";
echo "Array after updating fourth element value: <br>";
print_r($vehicles);
Output:
Array before updating an element value:
Array (
[0] => Tesla
[1] => Toyota
[2] => Audi
[3] => Nissan
[4] => Mazda
)
Array after updating fourth element value:
Array (
[0] => Tesla
[1] => Toyota
[2] => Audi
[3] => Volvo
[4] => Mazda
)
In the above example, we had Nissan as the fourth element and index 3. We used its index to update the array and assigned it a new value "Volvo". You can see that before and after updating the array, the value of array index 3 has changed from "Nissan" to "Volvo".
Example 2
Updating the value of an associative array element.
<?php
$country = array("name" => "Kenya", "continent" => "Africa");
echo "Array before updating an element value: <br>";
print_r($country);
echo "<br>";
$country["name"] = "Nigeria";
echo "Array after updating fourth element value: <br>";
print_r($country);
Output:
Array before updating name element value:
Array (
[name] => Kenya
[continent] => Africa
)
Array after updating name element value:
Array (
[name] => Nigeria
[continent] => Africa
)
We used the key 'name' to update its value from 'Kenya' to 'Nigeria'.
In the scenario where we already know an array element value but not its index or key we can update it by first getting its key|index using the array_search() function. We then use the found key or index in the same way as in the examples above. This works only when the array has one instance of that value.
Syntax
array_search(value, array)
Return value: The index or key of the array element matching the value.
Parameters
Parameter | Requirement | Description |
---|---|---|
value | Required | Specifies the value whose key|index we are searching. |
array | Required | Specifies the array from which we are searching a matching value in elements. |
Example
<?php
$colors = array("Black", "White", "Green", "Red", "Blue");
$index = array_search("Green", $colors);
echo "Array index for Green is $index";
Output:
Array index for Green is 2
In the example below, we will update the array to replace the value Red with Orange.
<?php
$colors = array("Black", "White", "Green", "Red", "Blue");
echo "Colors array before updating: <br>";
print_r($colors);
echo "<br>";
$colors[array_search("Red", $colors)] = "Orange";
echo "Colors array after updating a value: <br>";
print_r($colors);
Output:
Colors array before updating:
Array (
[0] => Black
[1] => White
[2] => Green
[3] => Red
[4] => Blue
)
Colors array after updating a value:
Array (
[0] => Black
[1] => White
[2] => Green
[3] => Orange
[4] => Blue
)
Updating multiple array elements at once in PHP
In some scenarios, you may need or be required to update the values of multiple array elements all at once. To do that, you will need to use the array_replace() function.
The array_replace() is an in-built PHP function that replaces the values of an array with values of another array having the same keys or indices.
Syntax
array_replace(array1, array2)
The function returns an array, or null if an error occurs.
Parameters
Parameter | Requirement | Description |
---|---|---|
array1 | Required | Specifies the array in which elements are to be replaced |
array2 | Required | Specifies the array(s) from which the replacement elements will be extracted. Values from later arrays overwrite the previous values. |
If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later arrays overwriting the previous values.
Example 1
Updating multiple elements in an indexed array using the array_replace() function.
<?php
$colors = array("Black", "White", "Green", "Red", "Blue");
echo "Colors before updating: <br>";
print_r($colors);
echo "<br>";
$replacements = array(0 => "Purple", 4 => "Yellow");
$colors = array_replace($colors, $replacements);
echo "Colors after updating: <br>";
print_r($colors);
Output:
Colors before updating:
Array (
[0] => Black
[1] => White
[2] => Green
[3] => Red
[4] => Blue
)
Colors after updating:
Array (
[0] => Purple
[1] => White
[2] => Green
[3] => Orange
[4] => Yellow
)
Example 2
Updating multiple elements in an associative array using the array_replace() function.
<?php
$country = array("name" => "Kenya", "continent" => "Africa", "city" => "Nairobi");
echo "Array before updating: <br>";
print_r($country);
echo "<br>";
$replacements = array("name" => "Rwanda", "city" => "Kigali");
$country = array_replace($country, $replacements);
echo "Array after update: <br>";
print_r($country);
Output:
Array before updating:
Array (
[name] => Kenya
[continent] => Africa
[city] => Nairobi
)
Array after update:
Array (
[name] => Rwanda
[continent] => Africa
[city] => Kigali
)
That's it!
Now you can comfortably update values of array elements in PHP.