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? 0:00 Programming is no different, and sometimes, 0:05 we'll need to edit an array element. 0:07 Maybe we misspelled a word, or maybe we want to specify HTML 5 and not just HTML. 0:09 An array is a type of variable, so you can update or 0:16 edit your array in much the same way as you would another variable. 0:19 Each individual element is also a variable, so 0:23 you can update or edit individual elements as well. 0:26 Let's remove this unset of the array. 0:31 Then I use the array variable $learn and set it equal to the string, 0:34 'my learning list'. 0:39 As we demonstrated before when adding values, 0:43 the learn list no longer contains an array. 0:46 It's a string with this new value. 0:50 You probably don't wanna change your array into a string. 0:52 So let's remove that line. 0:56 What you may want to change is one of the string values within your array. 0:59 To update a certain element within the array, 1:05 you specify that element using its key. 1:08 $learn = 'Email'. 1:11 My first element is now replaced with this new value. 1:20 But none of my other elements have changed. 1:25 There are many more ways to change and manipulate arrays. 1:28 Check out the teacher's notes for links to more resources. 1:32
You need to sign up for Treehouse in order to download course files.Sign up