PHP Basic Tutorial
To update an existing array item, use the index number for indexed arrays, and the key name for associative arrays.
Try it yourself
To update a value in an associative array, use the key of the element you want to modify.
Try it yourself
You can use the foreach
loop to iterate over an indexed array and modify each element.
The ampersand (&
) creates a reference, allowing you to modify the original array elements.
Try it yourself
Note
It's important to unset()
the reference after the loop.
If you don't unset()
it afterward, the reference can still point to the last item in the array even outside the loop.
Demonstrate the consequence of forgetting the unset()
function:
Try it yourself