در حال بارگذاری ...

Traditional Array Indexing0:00

Array destructuring in PHP 7.1 is probably one of the cooler features that you aren't using. So, let's imagine we have an array of people, and there'll just be a collection here. So we'll have one of John Doe, we'll just have first and last names here, and then we'll do myself here. Okay, so what we can do traditionally, if we want to filter through that, is say foreach people as person, and then var_dump, and if we want to grab the first item, yeah, we just reference the index, right? So we could say person[0], then a space, and then person[1] to get the last name. And sure enough, if I run this, we get John Doe and Jeffrey Way.

List() Destructuring Syntax0:29

So we could say person[0], then a space, and then person[1] to get the last name. And sure enough, if I run this, we get John Doe and Jeffrey Way. However, there's a couple things we could do in PHP 7.1 to clean this up. As a first step, we could destructure it by saying list($first, $last) = $person;. So what this basically does is it goes into the array right here, and it maps the first item to $first, and the second item to $last. So now what we could do if we want is say $first $last, and we're going to still get the exact same thing. So this is what's new in PHP 7.1. We can destructure into this nice short array syntax.

Destructuring in Foreach1:02

So this is what's new in PHP 7.1. We can destructure into this nice short array syntax. However, we can even take this further. In our foreach, we can destructure it right in the process, like this. foreach $people as $first $last, and now we get the exact same thing. Very cool. As we can see, the basic structure amounts to an array $first $last equals John Doe. Map this one to this, and this one to this. But now what if you're only interested in Joe, but not $first? Well, you can always use a comma here.

Skipping Unused Values1:34

But now what if you're only interested in Joe, but not first? Well, you can always use a comma here. So we can say, well, skip the first one. What I'm really interested in is the second match. So now if I var_dump last, we'll get just Doe. And you can do this for lots of things, not just arrays. For example, if you have an object that implements IteratorAggregate, then you can still get the exact same effect. For example, in Laravel, well, if we build up a collection of users or perform any query that returns a collection, you can destructure that.

Laravel Collection Destructuring1:56

For example, in Laravel, well, if we build up a collection of Users or perform any query that returns a collection, you can destructure that. So we could say $user1, $user2, $user3 equals this factory call. Or here's another example. Often we're using regular expressions to parse some kind of user input. So here's like a very, very trivial one for maybe a U.S.-based phone number. So look for 3 numbers dash 3 numbers dash 4 numbers. So we could compare that against 555-555-5555, and we'll save that to $matches, and then var_dump the results. And we don't get any.

Regex Matches Destructuring2:36

then var_dump the results. And we don't get any. Whoops, sorry. So we do get the full match, but maybe what you're interested in is the last four digits of the number. So we could wrap that within parentheses, and now we should have a second item here that contains the last four. Okay, well, once again, you might typically just say, well, the last four will be equal to that, and you dump it. However, this is a great use case for destructuring.

to that, and you dump it. However, this is a great use case for destructuring. You could say, I don't care about the first item, but I do care about the second match in the array. So we could do something along those lines, and then var_dump it, and run it, and we still get the same end result. Or of course, if you need it, you could say full match and the last four. So full, last four. And yeah, that's really all there is to it. Very simple, but at the same time, incredibly useful.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟