Defining Post Collection 0:00All right, welcome back. So today we're taking a look at PHP's various array functions, and you're going to use these all over the place. So take some time, learn them, and figure out why you would use them. I'll give you some examples. Let's start, imagine you have something like a post, just so we can have a collection of things to work with. And we'll say a post, we'll keep it very, very simple. A post has a title and a BooleanAnd we'll say a post, we'll keep it very, very simple. A post has a title and a Boolean that indicates if it has been published. Okay, so we will accept that title published, and then we will assign these, like so. Okay, next, we're going to set up a collection of posts. So I can say post equals a new array, and then we'll fill that up with various posts. Like my first post is published, and then duplicate that, how about a total of four times?Like my first post is published, and then duplicate that, how about a total of four times? My second, my third, and my fourth. And we'll say the fourth one has not been published yet, so I will set that to false. So now if we var dump our posts, and we take a look at this in the browser, let me quickly boot up a server. By the way, we could also do this directly from the terminal. So you could say php index.php, and we can get the output.By the way, we could also do this directly from the terminal. So you could say php index.php, and we can get the output. Or you can boot up a server, and now we can see them here. And in this case, by the way, I have a couple extensions installed, one called JSON Formatter for formatting JSON, which is a data type, and then another one called var masterpiece. Both of these are Chrome extensions,and then another one called var masterpiece. Both of these are Chrome extensions, just search for them, and then you'll have it installed in a few seconds. But yeah, either way, this is good. So now if I come back to Sublime, imagine that your boss tells you, well, thanks for the collection of posts, but I need some way to figure out which ones have not been published. Filtering Unpublished Posts 1:51but I need some way to figure out which ones have not been published. Okay, well, I'm gonna create a new variable called unpublished posts. And basically, I wanna filter this collection down to only those where the published status is set to false. Okay, so filter is our keyword there. Now, PHP has a bunch of array underscore functions. And in fact, you'll see with autocomplete, there's a bunch here.And in fact, you'll see with autocomplete, there's a bunch here. In reality, you're probably gonna use about five to 10, but it's fun to look around. So in our case, yeah, we want array filter. It'll accept our input, and then it's going to accept a closure that determines how we filter the current item. Okay, so here's how it works. You feed array filter your array of posts.Okay, so here's how it works. You feed array filter your array of posts. It's then gonna filter through them. So for each one, it's gonna trigger this function right here and then pass through the current post. So on the very first iteration, the post variable will be equal to this. On the second iteration, this. On the third, on the fourth. So now all we have to do is return a BooleanOn the third, on the fourth. So now all we have to do is return a Boolean that indicates if we want this object or item to be included in the result set. Think of it literally like a filter. That's what we're doing. So I want to include the post just as long as its published status is equal to false. So we can write it like this, or if you remove it entirely,So we can write it like this, or if you remove it entirely, then its value will be returned. So in this case, true will be returned, and then true, then true, and then false. But if you think about it, that's not quite what we want. Let's var dump unpublished posts. And I will run it again from the terminal. Okay, so notice we get the very first three, but not the fourth.Okay, so notice we get the very first three, but not the fourth. And that makes sense, right? What we said here is we want the object to be included in the result set just as long as it is published. But we want it if it's not published. So I can use a bang here to negate it. Okay, if we run it again, now I get an array of only the postsOkay, if we run it again, now I get an array of only the posts that have not been published. So that means if your boss said, well, now I want a collection of only my published posts, you can do the exact same thing here. You could say publish posts, and the only difference is we include the post in the result set if it is set to published. Okay, so if we run this one, Transforming with ArrayMap 4:15in the result set if it is set to published. Okay, so if we run this one, we'll get an array of three items, but not the unpublished one. So that's ArrayFilter. It allows you to filter down an array of items or objects or anything. So what about another one? How about ArrayMap? This is one of my favorites.How about ArrayMap? This is one of my favorites. So what if you have your collection of posts and you wanna filter through all of them, or we will say map, we're gonna map over them and we'll change the published status of each one to true. So this function, as you probably guessed, is Array underscore Map. But now I'm gonna show you one thing that sucks about PHP.is Array underscore Map. But now I'm gonna show you one thing that sucks about PHP. From time to time, just the way it was built up, it can be a little inconsistent. So take a look. With ArrayFilter, you give your array of items and then a closure, right? For ArrayMap though, you give it a closure and then the array of items. So they're reversed.and then the array of items. So they're reversed. And this can be kind of annoying. In reality, you will probably end up using, especially once you get into the industry a bit more, you're probably gonna be using wrappers around these functions. And we call those collection classes. So those collection classes kind of encapsulate or isolate the inconsistencies that PHP offers.So those collection classes kind of encapsulate or isolate the inconsistencies that PHP offers. But still nonetheless, it's kind of annoying. But anyways, we're gonna give it a function and now the items will be the array of posts. So if I wanna map over these, here's the way it works. What you return from this function will be assigned to that current item in the array. So if I were to do something like this, modified, and then we var dump the modified array,So if I were to do something like this, modified, and then we var dump the modified array, what do you think you're gonna get here? Well, you're gonna get an array where every single item is equal to foobar. So that's how it works. What you return here is what gets stored for that current iteration. So if we want, we could say post published equals true and then return the post.So if we want, we could say post published equals true and then return the post. That would be fine. So if I run it now, yes, you'll get your collection of posts but now every single one has its publish status set to true. However, really in this particular case, we're just modifying it. So you just as easily could use a simple for each. For each post as post, then we'll set the post title, or I'm sorry, post published to true, like so.For each post as post, then we'll set the post title, or I'm sorry, post published to true, like so. Now, if you var dump posts, we should get the exact same thing and we do, but we didn't need to use any special array function. So when exactly does array map end up being useful? And like I said, it's when you need to modify what gets returned. So while yes, this is totally fine and it'll work, what you might end up doing is, yeah, modifying it,So while yes, this is totally fine and it'll work, what you might end up doing is, yeah, modifying it, transforming it in some way. Maybe you have a post object but you really just need an array. So we can use this casting syntax here. Okay, so now we've transformed it in some way. If we run this again, now you'll see that I don't have a collection of post objects, I have a collection of arrays.now you'll see that I don't have a collection of post objects, I have a collection of arrays. Or maybe you only needed a subset. So you could just return title is post title. Maybe for whatever reason, you don't want to expose everything about the post. You just want to expose the title and maybe one other field. Anyways, now if we run it, we have a subset of that data. Okay, so that's array map. Another one, we'll do one more. Extracting Values with ArrayColumn 7:45Okay, so that's array map. Another one, we'll do one more. We could do array column. So what happens if your boss says, can you give me very quickly just the titles of all of my posts? Okay, well, we can use array column for that. Or actually, in fact, we could use array map if you want, as you'll find a lot of times multiple tools or methods or functions will get the job done.as you'll find a lot of times multiple tools or methods or functions will get the job done. It's just a matter of which one feels most appropriate. So like I said, yeah, I mean, you could do this. Accept the posts and then return the post title. And then we could say titles. Okay, so if I var dump the titles here and we run it and it fails, if we come back, sorry, post. So yeah, fair enough. We can get the array of titles in that way.So yeah, fair enough. We can get the array of titles in that way. But I'd say maybe a more appropriate function to use is array column. Here, you provide your collection and then you provide the key of the array or the public property of the object that you wanna fetch. So in this case, we want the title property, right? Okay, let's try it out. Titles.Okay, let's try it out. Titles. And notice in this case, yeah, there's no closure to pass. You can just reference the key. So now if I var dump the titles, and if we run it again, we'll get the exact same thing. But do you see what I mean? How this feels a little more appropriate and it's less code. Now, just a quick note on that though,and it's less code. Now, just a quick note on that though, it would need to be public. So if we were to run that again, yeah, you can't access it if it is protected or private. So yeah, this will work with objects, but it also can work with simple arrays. So let's imagine that we don't have a array of post objects. We just have an array of arrays. Okay, well, this once again is a great use for array map.We just have an array of arrays. Okay, well, this once again is a great use for array map. Because if you think about it, if you did this, let's just try it. For each post is post, I want to turn the post into an array. So what you might have to do is something like this. You might have to say post index equals a cast version of post. So yeah, I mean, now, if we were to take a look at this and run it,So yeah, I mean, now, if we were to take a look at this and run it, yes, that did work. We got an array of arrays. But do you see what I mean? How it just feels a little sloppy. It'll do the job and it's fine, but I'd rather use array map. So we'll say post, we're going to map over it, accept the collection,So we'll say post, we're going to map over it, accept the collection, and then just return a array version of the post. So now I'm gonna get the exact same thing and this feels like a better choice to me. Anyways, we could then use array column on this. So I wanna grab the title for each post and save it into this titles variable. Now, if we run it, we're gonna get the exact same thing. And this is just to show you that in the top example,Now, if we run it, we're gonna get the exact same thing. And this is just to show you that in the top example, which I guess I've now deleted, we were fetching a public property called title. But now in this example, we're fetching the key called title. So if we take a look at our modified array real quick, yeah, up here, we're fetching the title key. So if you instead wanted, for example, the author, maybe we'll add that public author like so, and then assign it.maybe we'll add that public author like so, and then assign it. Okay, so now we'll just use initials here. Mine, mine, somebody else, and then another person. Okay, so if we now just want an array of the authors, we could say array column posts, and then give me the author. Okay, if I var dump that, we now have, right up here, by the way, we're getting the old posts. Anyways, we now have an array of all of the authors.right up here, by the way, we're getting the old posts. Anyways, we now have an array of all of the authors. But here's one last cool thing you could do. You can set the third argument to what you wanna be used as the key. So if I said we wanna use the title of the post as the key for the array, then we could do that. So now you'll see that the key is equal to the post. So that's kind of fun to know. Okay, so I think that'll do it for this episode. Recap and Practice Exercise 11:59So that's kind of fun to know. Okay, so I think that'll do it for this episode. So you got your first dose of working with array functions. Specifically, array filter allows you to filter down a collection, and then return a Boolean to indicate if that item gets included in the result set. Next, you have array map. This allows you to transform an object in some way. So map over this collection of posts,This allows you to transform an object in some way. So map over this collection of posts, and for each one, yeah, maybe you're returning a subset of data. Maybe you're converting it. Maybe you're just wrapping it within some other object. So maybe something like this. Maybe you have a post object, and maybe somebody else supplied that. But you wanna use your own post objectand maybe somebody else supplied that. But you wanna use your own post object because it has extra functionality that you want. So you can map over their collection, and for each one, return a new instance of your collection using their data. That's a little more advanced. I know that may sound kind of confusing, but that's a common pattern. And then finally, array column,but that's a common pattern. And then finally, array column, which allows you to pull a value from each item in the collection. So in this case, we are pulling the author from each item in the post collection. All right, keep playing around with this and have fun. Maybe use the concept of a task. So create a class called task, and maybe that has the description of the task,So create a class called task, and maybe that has the description of the task, whether it has been completed or not, and then figure out if you can filter down an array of tasks according to various criteria. Have fun.