We can make adjustments to existing arrays—we don’t have to create a new array when we want our array to change.
We add elements to the end of an array by taking the variable name and appending square brackets (
), the assignment operator (
=), and the element we want to add:
$string_array = ["first element", "second element"]; $string_array = "third element"; echo implode(", ", $string_array); // Prints: first element, second element, third element
We can also reassign the individual elements in an array:
$string_array = ["first element", "second element", "third element"]; $string_array = "NEW! different first element"; echo $string_array; // Prints: NEW! different first element"
In the code above, we replaced the original string held in the array (
"first element") with a new string value:
"NEW! different first element".
We’re going to have you make several changes to the provided
$change_me array without making any changes to how it’s initially declared (line 4).
First, add a string element to the end of the array.
Great! Add another element to the end of the array. This time it should be an integer.
Reassign the 1st index location of the array (which currently holds the value
6) to hold the value
Awesome! Let’s take a look at this array of yours. Print your array to the terminal.