Adding Array Elements5:08 with Alena Holligan
I don't know about you, but I'm constantly adding things to my todo list. In this video we'll look at three ways php gives us to add elements to an array -- two of which let you add elements to the end of the array, and one which lets you add elements to the beginning of the array.
Now, that we have our first array set up. 0:00 Let's look at some ways to make changes to the contents of that array. 0:02 We can add, remove, and edit elements of our array. 0:06 We'll be using some more built in PHP functions, so 0:10 it can be helpful to pull up the manual for these functions. 0:13 The first thing we should look at is how to add new values to the array. 0:17 I don't know about you, but I'm constantly adding things to my to do List. 0:22 I'll show you three ways PHP gives us to add elements to an array. 0:26 Two of which let you add elements to the end of the array, and 0:30 one which lets you add elements to the beginning of the array. 0:34 If you want to add one element at a time, 0:38 the easiest way to do that is to use the following format. 0:41 The array learn, followed by opening and closing square brackets, 0:45 the assignment operator equal and then our new value, 0:49 build something awesome. 0:53 Let's use the var_dump instead of implode, so we can see some more details. 1:00 Now, let's run our script. 1:06 Now, we see all four elements and PHP automatically assigned 1:11 a numerical key to the new element by incrementing the largest 1:16 previously used integer key, 1 more than 2 is 3. 1:21 The square brackets tell PHP to add another element to the array. 1:25 If instead of using square brackets, we just assign to learn 1:32 We override the entire array and set learn equal to this new string. 1:40 Let's remove that line. 1:46 Another way to add elements is by using one of PHP built in array functions. 1:50 Let's check out the manual for these functions. 1:56 Open a browser and search for PHP array functions. 1:59 Click on PHP array functions manual. 2:06 As you can see there are many functions available to use with arrays. 2:09 We'll be covering a few of these functions in this course, but 2:14 it's a good idea to familiarize yourself with what's available. 2:17 The first function we'll look at is array push. 2:21 This is a good choice, if you wanted to add more than one element at a time. 2:26 You first specify the array, and then add additional elements each 2:30 separated by comma, just like when we originally assigned values. 2:36 You can scroll down to see an example. 2:41 Now let's go back to work spaces and add some more items to our list. 2:46 Array_push, the name of our array learn, and then our new items. 2:49 Functions, forms and objects, 2:58 and let's run the script. 3:04 We can see these new values added to the end of the array. 3:11 Both of these options added elements to the end of the array. 3:16 There is a third option that lets us add elements to the beginning of the array. 3:20 Let's go back to the manual. 3:25 If we scroll down, you can see the see also heading, 3:28 and there you'll find array_unshift. 3:33 Array_unshift works in the same way as array_push. 3:36 First, we specify the array, and then we add additional elements. 3:41 Each separated by a comma. 3:46 Let's go back to work spaces. 3:48 Let's add array unshift, 3:50 specify our array, learn, and 3:55 then add our elements, HTML and CSS. 4:00 Now let's pull the console up, And run our script. 4:08 Now we can see these two elements added to the beginning of our array. 4:15 The main difference is that array unshift will add elements to the beginning of 4:20 the array, while array push added the elements to the end of the array. 4:25 Another difference is how these functions affect the array key index. 4:30 Array push will add the next index number in order, so 4:35 if the array keys are one, three, five and 4:40 you add another with your a push, you will have one, 4:44 three, five, six array_unshift will update the index. 4:49 If you add another one to the beginning now will have zero, one, two, three, four. 4:55 We'll take a closer look at how the index affects arrays in the next video. 5:03
You need to sign up for Treehouse in order to download course files.Sign up