To update an existing array item, use the index number for indexed arrays or the key name for associative arrays.
Replace the second array item “BMW” with “Ford”:
$cars = array("Volvo", "BMW", "Toyota"); |
Note: The first item is at index 0. |
To update items in an associative array, use the key name.
Change the year to 2024.
$cars |
There are various techniques for changing item values in a foreach
loop.
One approach is to use the &
character in the assignment to assign the item value by reference. This ensures that any modifications made to the array item inside the loop will affect the original array.
Replace ALL item values with “Ford”.
$cars |
Note: Remember to include the Without To illustrate this, observe what happens when we change the value of |
Show the consequence of forgetting the unset()
function:
$cars $x var_dump($cars); |