‫So now we're going to look at multi dimensional arrays which are arrays within arrays.
‫And like I said in the last video it's common to fetch data from a database and store it in an array
‫of associative arrays.
‫So I have my template here.
‫Let's start off with something simple.
‫I'm going to create a variable here called fruits.
‫And we're going to set it to an array.
‫And inside here I'm going to put some some other arrays.
‫So this one will say Apple.
‫Read.
‫So there'll be an array with the fruit and the color.
‫So here's another array.
‫Say orange.
‫Orange.
‫And let's do.
‫Banana.
‫And yellow.
‫Okay.
‫So each one of these are an array inside of the fruits array.
‫Now let's look at how to access some of this stuff.
‫So if we want to access Apple.
‫And if you want to pause the video and try it on your own.
‫If you guys are brand new to this and you know you haven't done this before, but you think you might
‫know how this works, go ahead and try.
‫Pause the video.
‫Try things out.
‫If it doesn't work, it's fine.
‫So again, let's say I want to get I want to get this apple right here this string.
‫So let's set the output variable to fruits.
‫So fruits is the entire array.
‫And we want the first element in that array which is this array.
‫So to get that array we'll use zero.
‫Now to get the first item in that sub array we want to get zero again.
‫So if we save that reload, we should see Apple.
‫If I want the color red, then I would get the first index in that.
‫For the second index in that first array reload.
‫Then we get red.
‫All right.
‫If I wanted to add let's say add a fruit a fruit and a color I could say fruits brackets set that to
‫let's say grape.
‫Purple.
‫And then let's let's output down here.
‫We'll just do paragraph and pre tags.
‫Print are.
‫Fruits.
‫Okay.
‫So you can see for the last item here we added that that array with grape purple.
‫Now let's do something a little more realistic.
‫So let's say we have an array of users.
‫So this is actually going to be an array of associative arrays.
‫So in here let's do our first associative array.
‫So it's a user.
‫So it's going to have a name.
‫And we'll set that to John.
‫Can I have an email?
‫Set that to.
‫John at gmail.
‫And let's do password.
‫Okay.
‫So that's our first user.
‫And then we separate these with commas.
‫And I'm going to just copy that down a couple times.
‫We'll change out the name here.
‫Let's say Mary.
‫Change the email.
‫Here.
‫We'll say, I don't know, Ken.
‫Change the name.
‫Okay, so this is.
‫Excuse me.
‫This is more like, realistic of something that you would get from a database.
‫So let's show this instead of the fruits.
‫So down here I'm just going to.
‫Say users reload.
‫And now you can see we have.
‫Each array, each associative array that has name, email and password.
‫And if I wanted to access, let's say, Mary's email, then I could do let's set output to users.
‫And I want the.
‫Um.
‫Mary's.
‫What?
‫This the second one.
‫So she's the one index.
‫So let's say one.
‫And then we want to open up brackets with the field that we want, which would be email.
‫So if I reload that, then in the output space we should see Mary at gmail.
‫If we wanted to add a user.
‫Let's.
‫Yeah, we'll just come down here and say.
‫User.
‫Our users rather and then some brackets and set that.
‫Let me just copy one of these.
‫So I'll paste that in there.
‫Let's say Mike.
‫Mike.
‫And then if I save that, we take a look at our array.
‫Whoops.
‫Something's not right here.
‫I forgot my semicolon.
‫So now we have Mike added to the array.
‫And that's just one way to add.
‫Remember all the methods we looked at we could use array push for example.
‫So I'll grab.
‫That and let's say array.
‫Uh.
‫Array.
‫Underscore.
‫Push.
‫And I want to push on to users.
‫And then I'm going to paste that in and just change the name.
‫To, let's say, Larry.
‫Reload and Larry gets added.
‫Okay, so there's always multiple ways you can do the same thing.
‫If we wanted to remove the last user, for instance, in this case Larry, we could use array underscore
‫pop not not poop.
‫We don't want the array to poop.
‫So let's pass in the name of the array users.
‫And now if I reload Larry goes away.
‫All right.
‫If I want to remove the first user array shift.
‫Pass in, users reload and the first one is John.
‫So now John's gone.
‫If I wanted to remove a specific user, then I could just set.
‫Or I should say unset that user.
‫So let's say users.
‫Zero, which in this case will be Mary.
‫So if we reload now, Mary's gone.
‫All right.
‫So we can use all the standard array functions on an array of associative arrays.
‫If we want to count the number of users we could do that.
‫So let's set.
‫Output equal to count users, which at this point is only two, because we've added we've removed a
‫bunch, so we have to.
‫All right.
‫So in the next video we're going to do a couple small array challenges.