Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Editing Array Elements1:36 with Alena Holligan
Have you ever misspelled a word? Or maybe changed your mind about something? Programming is no different, and sometimes we'll need to edit an array element. In this video we'll see how to modify an element by specifying it's key.
Additional Option for Modifying an Array
There are many Array Functions for modifying arrays. A few to get you started:
array_splice: remove a portion of the array and replace it with something else.
array_combine: creates an array by using one array for keys and another for its values.
array_merge: merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
Have you ever misspelled a word or maybe changed your mind about something?
Programming is no different, and sometimes,
we'll need to edit an array element.
Maybe we misspelled a word, or maybe we want to specify HTML 5 and not just HTML.
An array is a type of variable, so you can update or
edit your array in much the same way as you would another variable.
Each individual element is also a variable, so
you can update or edit individual elements as well.
Let's remove this unset of the array.
Then I use the array variable $learn and set it equal to the string,
'my learning list'.
As we demonstrated before when adding values,
the learn list no longer contains an array.
It's a string with this new value.
You probably don't wanna change your array into a string.
So let's remove that line.
What you may want to change is one of the string values within your array.
To update a certain element within the array,
you specify that element using its key.
$learn = 'Email'.
My first element is now replaced with this new value.
But none of my other elements have changed.
There are many more ways to change and manipulate arrays.
Check out the teacher's notes for links to more resources.
You need to sign up for Treehouse in order to download course files.Sign up