‫All right guys.
‫So now we're going to look at associative arrays which are used to store key value pairs.
‫And the keys can be strings or numbers.
‫The values can be of any type.
‫And if you're coming from the JavaScript world, associative arrays are similar to object literals or
‫dictionaries.
‫In Python they're used for the same same type of stuff.
‫So I'm starting off just with my template and my output variable.
‫And I'm going to create an associative array called user.
‫So let's say user we're going to set that to some brackets here.
‫And then we're going to have our key value pair.
‫So instead of having the index or the key as zero or something like that, we're going to have this
‫key be a string of name okay.
‫So we'll have a name.
‫And then let's do an email.
‫So email and you want to use this this fat arrow syntax the equal sign and the angle bracket.
‫So we'll say John at gmail.com.
‫And let's say.
‫Password.
‫Just set that to.
‫Obviously, if you were working in a database, your password would be hashed.
‫And then and these can be any, any value, any data type.
‫They could be numbers, it could be boolean, they could even be arrays.
‫So let's add hobbies.
‫And let's set that to be an array within our associative array.
‫So we'll say tennis.
‫So string of tennis and a string of video games.
‫So now we have an array as a value.
‫So if I try to echo this right, so let's say echo user, let's see what we get.
‫So we get an error.
‫So just like with a regular array we can't echo it.
‫So let's do a var dump.
‫So var underscore dump user.
‫And there we go.
‫So you can see all the keys, the values, the data types and so on.
‫Instead of doing a var dump up here, let's output this down in our template just to look a little better.
‫So we'll have an H to let's say text Excel and font dash semibold.
‫And let's do margin MY4 and we'll say user array.
‫And then we're going to have some p tags.
‫And inside that we'll have some pre tags.
‫And inside that, let's open up PHP and we'll say print underscore R and pass in our user.
‫Like that.
‫Okay, so now we can see things a little more clear and you can see the hobbies array as well.
‫So let's look at how to access elements.
‫So I'm going to come down here and we'll set the output.
‫Because whatever I put in the output variable is going to display here.
‫So let's say I want to get the name of this user.
‫So what we would do is say user and then in brackets name.
‫So let's reload that.
‫And now we should see John.
‫Okay.
‫If I wanted to get the email.
‫We could do that.
‫That gets the email.
‫So if we try to access an element that doesn't exist, let's say email to.
‫Then we're going to get an error that says undefined key email to okay, that key is not defined.
‫So let's say we want to access nested elements.
‫We want to access the tennis in the hobbies array.
‫So we could do that by saying user.
‫And then we want to go into the hobbies array.
‫And then we want tennis which has which is at the zero index.
‫And you could put an associative array within an associative array as well.
‫So if I go ahead and reload that we should see tennis.
‫Now if you want to add a new element you can do that as well.
‫So let's say user.
‫And let's add address.
‫So we'll say the address.
‫We want that to be 123 Main Street.
‫And if I save that and reload and we're printing the array down below, you'll see that the address
‫gets added.
‫And I can also unset the address.
‫Just like in the last video we looked at unset.
‫So we can say user.
‫Address.
‫And reload.
‫And now that's gone.
‫All right.
‫So pretty simple.
‫And you'll be using associative arrays a lot when you deal with databases.
‫You know when you fetch data from a database, in a lot of the times you'll get it as an associative
‫array, you'll get the users or blog posts, whatever it might be.
‫And in many cases, you're going to get an array of associative arrays.
‫And those are called multidimensional arrays.
‫So that's what we're going to look at in the next video.