1 00:00:00,670 --> 00:00:03,520 Adding things to a to-do list is great. 2 00:00:03,520 --> 00:00:06,680 Unless we don't actually do anything on our to-do list, 3 00:00:06,680 --> 00:00:08,720 then it hasn't helped us at all. 4 00:00:08,720 --> 00:00:11,610 Let's hope that our to-do list is getting smaller because we're 5 00:00:11,610 --> 00:00:13,350 actually getting stuff done. 6 00:00:13,350 --> 00:00:16,460 So we need to be able to remove things from our list. 7 00:00:16,460 --> 00:00:21,030 Let's look at three ways provided by PHP to remove elements from an array. 8 00:00:23,020 --> 00:00:28,775 First, in opposition to array_unshift, we also have array_shift. 9 00:00:30,610 --> 00:00:33,070 This removes the first element from the array. 10 00:00:34,115 --> 00:00:36,797 array_shift only takes one parameter, 11 00:00:36,797 --> 00:00:40,022 the array from which to remove the first element. 12 00:00:40,022 --> 00:00:42,854 Let's go back to workspaces. 13 00:00:42,854 --> 00:00:49,177 We'll add array_shift and then our array $learn. 14 00:00:49,177 --> 00:00:53,850 array_shift doesn't just zap this element out of existence. 15 00:00:53,850 --> 00:00:58,399 First it will return the element so you can use it for whatever you want. 16 00:00:58,399 --> 00:01:02,350 Maybe move it to a new array or display a message of what was removed. 17 00:01:03,500 --> 00:01:08,639 We can either assign it to a variable or we can use it directly in our message. 18 00:01:08,639 --> 00:01:13,353 Let's echo 'you removed ' and 19 00:01:13,353 --> 00:01:16,500 then array_shift. 20 00:01:18,240 --> 00:01:19,020 Now let's run our script. 21 00:01:20,530 --> 00:01:23,930 We see you removed HTML and 22 00:01:23,930 --> 00:01:29,820 we can also see that the HTML element is no longer in our list. 23 00:01:29,820 --> 00:01:34,300 We can also remove elements from the end of the array using array_pop. 24 00:01:35,350 --> 00:01:39,968 array_pop will also return the element before it's removed. 25 00:01:39,968 --> 00:01:43,056 So let's duplicate this line and 26 00:01:43,056 --> 00:01:47,938 we'll replace array_shift with array_pop. 27 00:01:49,755 --> 00:01:51,040 Now let's run the script again. 28 00:01:53,150 --> 00:01:57,810 We can see that we removed HTML and Objects from our list. 29 00:01:59,580 --> 00:02:05,320 Finally, you can remove any specific array element as long as you know the key. 30 00:02:06,340 --> 00:02:12,140 To unset the second element of the array, we would use unset, 31 00:02:12,140 --> 00:02:18,740 the name of our array, $learn, and the element's key. 32 00:02:19,900 --> 00:02:23,450 The key of the second element is 1. 33 00:02:23,450 --> 00:02:27,830 unset will take multiple parameters. 34 00:02:27,830 --> 00:02:30,710 So you can unset more than one element at a time. 35 00:02:32,460 --> 00:02:35,197 We can add, learn[2], 36 00:02:35,197 --> 00:02:40,437 which would unset the second and third parameters. 37 00:02:43,402 --> 00:02:50,030 Now you can see that the keys one and two are no longer in our list. 38 00:02:50,030 --> 00:02:54,080 And their values of conditionals and arrays are gone as well. 39 00:02:55,230 --> 00:02:58,990 I want to point out that the other functions that we've been using 40 00:02:58,990 --> 00:03:03,140 updated the array keys, but unset does not. 41 00:03:03,140 --> 00:03:09,680 So when we removed keys one and two, it left a hole between keys zero and three. 42 00:03:10,870 --> 00:03:13,050 This could be the result you want. 43 00:03:13,050 --> 00:03:17,110 For example, if this list is in order of priority and 44 00:03:17,110 --> 00:03:22,380 you remove an item when it's complete, knowing which number and 45 00:03:22,380 --> 00:03:25,820 how many have been removed could be helpful. 46 00:03:25,820 --> 00:03:30,260 In this example, I know that items one and two are complete, so 47 00:03:30,260 --> 00:03:32,220 I should really work on item zero. 48 00:03:33,300 --> 00:03:35,190 If you never use the key but 49 00:03:35,190 --> 00:03:39,290 just print out a list of items, the keys themselves don't really matter. 50 00:03:40,370 --> 00:03:44,710 However, another example would be with a top ten list. 51 00:03:44,710 --> 00:03:47,550 When an item gets pushed out of its position, 52 00:03:47,550 --> 00:03:51,340 you want to shift the positions of all the items. 53 00:03:51,340 --> 00:03:56,660 You always have ten items and you want to pull a specific item by position. 54 00:03:57,730 --> 00:04:02,039 This requires re-indexing the array keys. 55 00:04:02,039 --> 00:04:08,277 We do this using the array_values function, 56 00:04:08,277 --> 00:04:13,849 learn = array_values($learn). 57 00:04:13,849 --> 00:04:18,350 array_values creates a new array out of the array values. 58 00:04:18,350 --> 00:04:22,286 Since we're assigning the results back to our learn array, 59 00:04:22,286 --> 00:04:24,999 it works like a refresh of our array keys. 60 00:04:27,773 --> 00:04:32,720 unset can be used with any variable, not just array elements. 61 00:04:32,720 --> 00:04:38,255 You can also unset the entire array, effectively destroying the array, 62 00:04:38,255 --> 00:04:45,210 unset($learn);. 63 00:04:45,210 --> 00:04:49,760 Now we see an error because the variable, $learn, no longer exists.