PHP Array Functions8:11 with Hampton Paulk
Arrays are super flexible data type that allows us to store and organize several different data types. With this flexibility, we will need lots of ways to work and manage array data.
Arrays are a super-flexible data type that allow us to store and 0:00 organize several different data types. 0:04 With this flexibility, we'll need lots of ways to work and manage array data. 0:07 Let's look at some of those in workspaces. 0:11 We're gonna take a look at two different array functions. 0:14 The first one is called array_keys and then the next one is called array_walk. 0:17 All right. 0:27 So we're going to take a look at these two functions. 0:28 And I'm actually gonna comment them out for now and we'll get back to that. 0:31 But what we're going to do now is jump over to the DOCs now and 0:35 look for array_keys. 0:39 So I already have it open here and 0:41 you can find it at php.net/manual/en, for English, /functions.array-keys.php. 0:45 The link will be in the show notes. 0:50 So taking a look at the definition here, 0:53 it says the array_keys will return all the keys or a subset of the keys of an array. 0:55 In the description you can see that the output is going to be an array. 1:01 The output is going to be an array of all the keys, 1:05 numeric and string, from the array. 1:08 It has one required argument which is the array itself. 1:11 So we're gonna need to create an array to start with. 1:14 So let's do that now. 1:17 All right. So, we're gonna go up to the top here, 1:19 come down a few lines, and create an array, and we call it names. 1:21 So inside of our array we are actually going to have keys and values, so 1:25 it is an associative array. 1:31 And so our first key is going to be the actual name of the person, so 1:32 we'll say Mike. 1:37 And then the value of Mike, or of Mike's name there will be his title. 1:39 So let's say frog. 1:45 All right, do a comma here. 1:47 Actually use the proper syntax, which is a pointer here or an arrow. 1:50 And then we'll do a few more here. 1:54 So, let's say, Chris. 1:56 And then we'll do the operator. 1:59 And then the title, which will be teacher. 2:02 And then we'll do one more. 2:05 Then we do Hampton. 2:09 And then we'll do teacher. 2:10 Okay, so we have a couple of different names here Mike, Chris, and Hampton. 2:16 And then a few titles frog and then teacher. 2:21 So what we're gonna do is now use array keys to walk through this. 2:24 So we will go here and uncomment at this line and 2:29 we're actually going to var_dump it. 2:32 Because it's going to return to us an array, and 2:35 we're not just going to print it right off. 2:37 So the argument that we pass through, 2:39 according to our documentation, is just the array itself. 2:42 So we'll pass through $names, hit Save, and then go over and preview this. 2:46 So here we can see it is an array. 2:53 It has three different values. 2:55 So the first one is Mike, the second one is Chris, and the third one is Hampton. 2:57 So those are the keys from that array. 3:01 So, let's take a look and compare that. 3:05 All right, so let's see, four, five, and six, we have Mike, Chris, and Hampton. 3:06 Those are our keys. 3:10 And then we go over here and we see Mike, Chris, and 3:11 Hampton are the actual values of this particular array. 3:13 So it returns them as values, which is great. 3:16 So if we really wanted to, we can actually say, foreach, and 3:19 then we'll say array_keys of names, and then as name, and then we'll do the close 3:25 there, get rid of our semicolon, open and close for the for each. 3:32 And then we can just do say, for instance, echo. 3:37 And then, hello comma, and then the person's name. 3:40 Close it with a double quote and then a semi-colon. 3:46 Now let's take a look at our preview again. 3:49 All right, hello Mike, hello Chris, hello Hampton. 3:50 Again to solve that problem we can just add in a break tag. 3:54 Hit save and refresh. 4:00 Perfect. 4:03 So now we are able to take the keys. 4:04 Not necessarily the values from an array and 4:06 loop through them by using the array keys function. 4:09 The next step for us would be going through the other function here. 4:13 Which is called array_walk. 4:16 So what I'm going to do for now is, I'm actually going to comment this out. 4:17 All right. 4:22 And then go through and uncomment array_walk. 4:22 We need to actually find array_walk in the docs to see what it can do, so 4:25 let's switch back over to the browser. 4:29 Gonna close the preview window, and I'm gonna search for array_walk. 4:31 Okay. 4:37 Array_walk according to the definition here, 4:38 will apply a user supplied function to every member of an array. 4:41 So we have all of these different values or titles here, in an array. 4:46 We also have keys in an array. 4:50 So we wanna actually do something by passing everything in 4:52 an array through a function. 4:55 So, let's go ahead and do that inside of our workspaces. 4:57 I'm going to tab over. 5:01 We have three names each with a value of a title, so 5:03 we're going to have to create a new function here. 5:08 Actually, let's comment this out for a second. 5:11 And we're gonna create a function for them to walk through. 5:12 So I will call this a function. 5:15 And then we'll say print_info. 5:18 So our print info is actually going to be to need to know the value and 5:22 the key of the name's array. 5:26 So how do we get that information here? 5:29 Well, let's go back over to the docs. 5:31 And we'll take a look at our callback or our second argument. 5:34 So we're gonna scroll down and look at the parameters and read what it says here. 5:37 Typically callback takes two parameters. 5:41 The array parameters value is the first and the key or index is the second okay. 5:44 So when we create this, knowing we're gonna walk using this function, 5:50 we'll simply say that our value, okay, which is here, is going to be our first 5:54 argument, and the second argument, as it said, would be our key over our array. 5:59 All right. Heading down to line 14. 6:05 Now we're actually going to do something with this key and value pair. 6:08 So here we're actually going to echo the information that we need. 6:12 So we'll echo out and the first thing is actually going to be our key. 6:15 Which is our names. 6:20 So we'll say, key is a, and then 6:21 we'll pass through our value, which is in our case will be either frog or teacher. 6:27 So I'm gonna hit the period, then double quotes, and then close this out. 6:31 So now in order to walk through every instance of 6:36 our array through this function, we'll simply call array_walk. 6:39 And then we will say $name or $names rather. 6:44 And then the actual string version of our function which is print info. 6:49 All right close that out. 6:56 Close it with a semicolon. 6:56 Save it. 6:58 Lets switch back over and look at our preview. 6:59 Okay, Mike is a frog, Chris is a teacher, and Hampton is a teacher. 7:02 Again, we'll switch back over, change this to add our break in here, save it, 7:07 switch back over and refresh, and there we have it. 7:12 Mike is a frog, Chris is a teacher, and Hampton is a teacher. 7:15 The way we've done this, switching back over to our code, 7:18 is we've taken our names array, which starts on line three, and 7:21 then we've passed it through the function print info that starts on line 13. 7:24 All by using a single line of code when we call this by using line 17, 7:30 array_walk $names and then print_info. 7:36 All right, last little bit switching back over. 7:39 If we take a look here on the right in the docs you'll see that 7:43 there are quite a few array functions. 7:47 I definitely suggest going through and reading through some of these. 7:49 Looking through array keys, mapping things padding or 7:53 popping out values as well as stuff like array combined. 7:56 Combining two different arrays by using one array for the keys and 8:01 another for the values, these can be very powerful functions and 8:04 just knowing they exist will help you greatly as a developer. 8:07
You need to sign up for Treehouse in order to download course files.Sign up