Removing Array Elements4:50 with Alena Holligan
Let's hope our todo list is getting smaller because we're getting stuff done, so we need to be able to remove things from our list. In this video we'll look at three ways php gives us to remove an element from an array.
array_shift: Shift an element off the beginning of array.
array_pop: Pop the element off the end of array
unset: Unset a given variable (works on more than array values)
Keep in Mind
unset($array); will remove your entire array.
Everytime you call array_shift you remove an element:
$removed = array_shift($learn); echo 'you removed ' . array_shift($learn);
Would remove two elements from the array.
Additional Option for Modifying an Array
Array Splice: remove a portion of the array and replace it with something else
Adding things to a to-do list is great. 0:00 Unless we don't actually do anything on our to-do list, 0:03 then it hasn't helped us at all. 0:06 Let's hope that our to-do list is getting smaller because we're 0:08 actually getting stuff done. 0:11 So we need to be able to remove things from our list. 0:13 Let's look at three ways provided by PHP to remove elements from an array. 0:16 First, in opposition to array_unshift, we also have array_shift. 0:23 This removes the first element from the array. 0:30 Array_shift only takes one parameter, 0:34 the array from which to remove the first element. 0:36 Let's go back to workspaces. 0:40 We'll add array_shift and then our array_($learn). 0:42 Array_shift doesn't just zap this element out of existence. 0:49 First it will return the element so you can use it for whatever you want. 0:53 Maybe move it to a new array or display a message of what was removed. 0:58 We can either assign it to a variable or we can use it directly in our message. 1:03 Let's echo 'you removed ' and 1:08 then array_shift. 1:13 Now let's run our script. 1:18 We see you removed HTML and 1:20 we can also see that the HTML element is no longer in our list. 1:23 We can also remove elements from the end of the array using array pop. 1:29 Array pop will also return the element before it's removed. 1:35 So lets duplicate this line and 1:39 we'll replace array_shift with array_pop. 1:43 Now let's run the script again. 1:49 We can see that we removed HTML and objects from our list. 1:53 Finally, you can remove any specific array element as long as you know the key. 1:59 To unset the second element of the array, we would use unset, 2:06 the name of our array, $learn, and the elements key. 2:12 The key of the second element is 1. 2:19 Unset will take multiple parameters. 2:23 So you can unset more than one element at a time. 2:27 We can add, learn, 2:32 which would unset the second and third parameters. 2:35 Now you can see that the keys one and two are no longer in our list. 2:43 And their values of conditionals and arrays are gone as well. 2:50 I want to point out that the other functions that we've been using 2:55 updated the array keys, but unset does not. 2:58 So when we removed keys one and two, it left a hole between keys zero and three. 3:03 This could be the result you want. 3:10 For example, if this list is in order of priority and 3:13 you remove an item when it's complete, knowing which number and 3:17 how many have been removed could be helpful. 3:22 In this example, I know that items one and two are complete, so 3:25 I should really work on item zero. 3:30 If you never use the key but 3:33 just print out a list of items, the keys themselves don't really matter. 3:35 However, another example would be with a top ten list. 3:40 When an item gets pushed out of its position, 3:44 you want to shift the positions of all the items. 3:47 You always have ten items and you want to pull a specific item by position. 3:51 This requires re-indexing the array keys. 3:57 We do this using the array values function, 4:02 learn = array_values ($learn). 4:08 Array_values creates a new array out of the array values. 4:13 Since we're assigning the results back to our learn array, 4:18 it works like a refresh of our array keys. 4:22 Unset can be used with any variable, not just array elements. 4:27 You can also unset the entire array, effectively destroying the array, 4:32 unset($learn);. 4:38 Now we see an error because the variable, $learn, no longer exists. 4:45
You need to sign up for Treehouse in order to download course files.Sign up