Project Setup in Vagrant0:00
Welcome back, and first on our list is to review Symmetric Array Destructuring, which sounds really fancy. Let's ignore that term entirely and just dig in. Now, I'm going to go into our Vagrant directory, and this is basically the folder that we installed in the last episode. So, in fact, I will open a new tab, and open this in Sublime. And then let's just create a source directory here, so that we can play around. So, we'll say, Vardav, hello. And if we now switch back to Vagrant, list the files, there's our source folder. Okay, so we can run php on that file, and we get it. So, what we'll do is review a script in 7.1, and then also run it. In this case, I have 7.0.
Intro to Array Destructuring0:39
So, what we'll do is review a script in 7.1, and then also run it. In this case, I have 7.0. And, of course, in this case, we get the same thing. That way, you can see what the differences are. Okay, so to start, Symmetric Array Destructuring. So fancy. So, we know that we can do things like this. For example, maybe we have a post array or something. And you have the first item is the title, and then the second item is the author of the book. You know, so we'll say, Joe Johnson. Now, if we save this to book, you may know that we can use the helpful list function.
Named-Key Destructuring2:18
If we try it, yeah, we get undefined offset. And, again, that's because the keys are named. So what we can do is something like this. We can say title, take this item, and the value associated with it, I want to store in a title variable. But that could be anything we want. They don't have to correspond there. Then we could do another one for the author, and we'll save that to an author variable. Okay, so now we're going to get exactly what we had before, which is very useful. And, again, for a measure, in PHP 7.0, it'll blow up.
Destructuring Nested Arrays2:41
Okay, so now we're going to get exactly what we had before, which is very useful. And, again, for a measure, in PHP 7.0, it'll blow up. Or, as a second example, imagine this. Let's say we have some books here, and this will be an array of arrays. The Martian, the author is, what, Andy Weir. And then one more, and why don't we just say The First Harry Potter by J.K. Rowling. Okay, so, of course, we could destructure this by saying something like title, and this time let's just save it to book. And then the author can be the same one here.
title, and this time let's just save it to book. And then the author can be the same one here. And we'll grab the first item there. Yeah, so right now this is essentially exactly what we've already reviewed. So if I go ahead and run this in PHP 7.0, it fails. In 7.1, same thing. However, we can also use this with a foreach. And, you know what, let's disable sublime linter. So we don't have to see all these errors. Okay, better.
Foreach with Destructuring3:39
So we don't have to see all these errors. Okay, better. Anyways, we could do a foreach here and say foreach $books as, and then, yeah, the exact same thing here. So, yeah, now if we've rdumped this, the title and the author, you'll see that we get the same thing for each item here. So let's run it again in 7.1. It all works, the title and the author for each item. And, of course, in 7.0, it fails. Okay, so really that's all you need to know here.
