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

Indexing Array Items0:00

All right, welcome back. Let's keep focusing on arrays. So you now know how to define an array, which is good, and you can loop over them. But yeah, what about situations where you need to interact with individual items within that array? For example, if I want to grab this value here from the books array, how do I do that? What is the appropriate syntax? And actually, it's not too hard. Within a paragraph tag, I want to echo out this item here. So I know I'm working with the books array, and if I count one, two, yeah, it's almost

Within a paragraph tag, I want to echo out this item here. So I know I'm working with the books array, and if I count one, two, yeah, it's almost like I want to do something like this. Go into the books array and give me the second item. And actually, that's not too far off. We can use brackets here, like this. But it's not going to give you the result you might expect. Let's have a look. Refresh. And yeah, I get Project Hail Mary instead of one, two, The Langoliers.

Introducing Associative Arrays3:08

And then once again, some kind of URL to buy the book. But there is one issue here. If I come back to this code six months from now, I may not remember what that URL is for. Is it a URL to purchase the book? Is it a URL to download the PDF after you purchased it? Is it a URL to the author's website? You know, you just don't know. And it would be nice if there was a way to associate a key with each value here. And in php, we can do that. It's called an associative array.

And in php, we can do that. It's called an associative array. Kind of a fancy term, but it just literally means associating a key with a value, like a key-value pair. So let's do this one. What would be a good key for this? Well, it's the name of the book, right? So let's call it name. And I create the association by making an arrow. So I have a fancy font that makes this arrow look pretty.

Looping Nested Arrays4:36

Conventions can be followed or ignored. You know, it's up to you. And we'll talk about that more in the future. Okay, so now let's do this one too. Here's the name, here's the author, and here is the purchase URL. Okay, I'm liking this. So now check this out. If I want to loop over it like we did in the last episode, well, once again, php foreach books as book, and I'm going to use the alternative syntax, and foreach. Now, in the last episode, we echoed out the book itself, right?

books as book, and I'm going to use the alternative syntax, and for each. Now, in the last episode, we echoed out the book itself, right? But that's not going to work. And already you can see my editor is kicking up a fuss. And we'll see that in the browser too. Yeah, so what's happening here is when we loop over the books array, each item now within that loop is not a string. It's an array itself. So we're basically saying, print the array to the screen. And php is going, huh, I don't know what you want me to do here.

Accessing Values by Key5:24

So we're basically saying, print the array to the screen. And php is going, huh, I don't know what you want me to do here. So it sounds like we actually want to print the name within that array. Okay, so you'll remember at the beginning of the video, we learned that we could access items within an array by using numbers. But when you're working with an associative array, you can instead use the key itself, sort of like an identifier. So in this case, let's replace it with name. And now we will get exactly what we had before. All right, very cool.

Array AccessKey-Value PairsData

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