PHP Tutorial (& MySQL) #7 - Arrays

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright then gang so now I'd like to talk about arrays so what are they exactly well basically arrays give us a way to still multiple different values inside just a single variable so you know how previously we've created a variable give it a name like name and set it equal to a value which could be a string now this value is being stored inside this variable right now an array gives us a way to create multiple different values and store them all inside a single variable now in PHP there's three types of arrays we have indexed arrays associative arrays and also multi-dimensional arrays so I'm going to cover the first two types of array indexed and associative in this video right here then in the next video we'll talk about multi-dimensional so then indexed arrays first of all these are the easiest to create and probably almost the most common as well so they're very simple to set up all we do is create a variable I'm going to call this people one and set it equal to an array by using square brackets this indicates an array in PHP so now what we have to do is place our values inside these square brackets so if we're doing an array of strings we could just do our first string first of all I'm going to say Shaun is the first person then we come and separate the values the second string could be crystal comment again and the third string is going to be right okay so now we have an array here which is storing three different strings all inside this one variable now say at some point we wanted to access one of these different values inside this array how do we do that well we do that using the index and that's why these are called indexed arrays so you know like when we talked about strings if we stored a string like name like so and then we wanted to find say this letter we'd say name and then square brackets one because the first one remember is zero position then this is one so that would find us the H now we do a similar thing inside arrays so instead of this being a string this is just now an array and this is position zero this is position 1 this is 2 and so forth okay so if we want to access something from this array we can just use square brackets to access the index of that element so for example we'd say people 1 and then 1 and that would get us this because this is position 0 1 2 ok so that's why these are called indexed arrays because we use the index right here to access them they all have a specific index associated with them 0 1 2 3 4 5 etc so let's just echo this through the browser and see if this works save that and come over here I'm going to refresh and we see crystal okay so that works okay so what I'm going to do is actually create now another indexed array called people too so people too is going to be equal to square brackets again in fact I'll show you a different way to create an array because sometimes you might see this this is one way to create an indexed array and that's probably the way I use most often and another way to create an array is by saying array and then in brackets because this is a function this is going to create an array for us we've seen functions briefly so they're inside we place our different values so I could have an array with Ken and then a comment again and then the second person will be Chun - Lee so this here this is an alternative way to create an array we use the array keyword the function and then we place our own values inside exactly the same way again I probably use this way more often than not book you will sometimes see me use this way as well because I tend to interchange them okay so let's try this one we'll say echo and we want people to and we'll get position I don't know one again which is this thing right here because 0 1 okay so let's save that and preview in a browser and this time we see Tinley okay so this works as well so that's two different ways now that we've seen to create an indexed array now it's not just limited to adding strings in a row we can also add other data types as well for example numbers so I could create another variable down here called ages and set that equal to an array this time using square brackets and I'll say 20 30 40 50 and now we have an array of numbers now I want to show you a different way to print out the values of these arrays because so far we've just been echoing out these single elements inside the array what if we wanted to echo the whole array well let me just try this echo Agis save this and preview over here and notice a ray to string conversion on line 12 array so it's basically not letting us do this because this right here should be a string whenever we echo something out it expects an easy conversion to a string okay then it can output to the browser so a better way to see these is to use a function called print underscore R and that means print readable so if I save this now it's going to print a readable version of this array onto the page over here so if i refresh now we can see we have an array position 0 is 21 is 32 is 43 is 50 so this is good if you want to see what's in an array while you're developing you can use this print our function right here and pass in the array that you want to print to the screen in a readable format okay cool so we have these different arrays now what if we want to maybe change one of the elements inside one of these arrays well to do that we use square bracket notation again so say for example I wanted to override this age right here with 25 well this is zero one position right one so we say ages and then one and then we set that equal to a new value so I could say ages one is now equal to 25 and now if we print our and then pass in ages this is going to update and show us that 25 is going to be in position one this time so save that refresh and now we can see 25 is in position 1 ok so that's how we overwrite a particular value inside right but what if we want to add a new value to the array well all we need to do I'm going to comment this out is say ages and then square brackets don't put anything in there and then we could say 60 or something like that and now what's going to happen is it's going to say okay well you've not passed in an index into this array that you want to overwrite so what I'm going to do is just add a new element onto the end which is going to be 60 now so again if we just print this to the browser we should see that so save refresh and now we see 60 on the end cool all right then now there's another way to add values onto the end of an array and I'm going to show you that now I'm going to comment this out and then I'm going to say array underscore push which is a function and this function takes in two parameters first of all the array that we want to push a value on to and that is going to be ages then the second value is going to be the value that we want to push onto the array so I'm just going to say 70 so I'm saying here look I want to use this array push method to add on a new element or a new value to the array at the end and this is the array I want to add something to and this is the value I want to add so if I save this refresh over here then we don't see anything because we've not printed it out so let's make sure we do that save it and refresh now we see this value added on to the end as well so that's two different ways we can add extra values onto an array now I want to show you one more thing when it comes to indexed arrays and that is going to be how to count the different elements inside it so say I want to know the length of this thing right here are the length of this thing then we use a method called count to do that so I'm going to echo the result of this to the browser so echo count and then we're going to pass in the age or the ages array and this should be now one two three four five six because we added these two on okay so we should see six in the browser refresh and we see six cool so that's how we count a raise and add new items onto the arrays override them and create them and this is all one type of array indexed arrays meaning we use the index of the element to access them or override them etc okay so that's the first type of array I wanted to show you and in fact I'm going to show you one more thing I'm going to show you how to merge two arrays together so say for example we have this array right here we have this array as well the both people and we want to merge them together so we take these values and we place them onto here into one gigantic array well we can use a PHP method to do that as well or a PHP function rather to do that first of all I'll create a third variable called people 3 and that is going to store the results of this function and the function name is a r8 underscore merge then this takes two arguments the first argument is going to be the first array we want to merge which is people want and the second argument is the second array we want to merge with it which is people too so now if we print our and people 3 we should see that merged array so save that for you in a browser and now we can see the merged array right there with every name in it cool ok then so that's indexed arrays now I'd like to move on to associative arrays and these are slightly different but not overly different so associative arrays are key value pairs so whereas in an indexed array we use the index like this to access the different elements inside the array this time when we're creating associative arrays we'll use keys instead of indexes ok and we'd specify those keys when we create the arrays so for example let me just put in brackets here key and value pairs so we remember what these are and let's create our first associative array so I'm going to create a variable called ninjas 1 and set that equal to an array right here now the way we do this is by first of all creating our key which is normally a string and this key is going to be the name of the ninja right and then we do equals and carrot which is like an arrow and these points to the value of whatever this key is going to be this will all make more sense when we come to work with it in a second but for example I'm gonna say black so I'm doing the ninja name and the belt color so Shan is a black belt right so this is the key and this is the value now if we were looking at indexed the value is this and the key is this so instead of the one I'm using now the string Shan and if we want to access this later on we're going to use this key to access the value I hope that makes sense so that is the first key value pair so we do a comma after that the second key value pair is going to be Mario and then an arrow and then the value which is going to be orange and sometimes by the way you might see spaces here really doesn't matter as long as the arrow is between the key and the value so comma and the third one I'm going to do is Luigi that's the third key and then an arrow and the value is going to be brown okay so that is our first now associative array okay then so say now I want to print out this value or echo out this value to the browser so the way at access this value is by saying first of all echo then the variable which is ninjas one then in square brackets we use the key which is Mario okay so inside a string we can't just say Mario like this it has to be inside the string inside quotations Mario okay so that will get us this value which is associated to that key so let's save that we should see orange on the screen and we do cool okay then so again if we wanted to print a readable version of this whole array to the browser then we can just say print underscore R and then ninjas want like so save that and preview in a browser and we see the whole thing so now we can see the key here and the value the key the value etc so these were before zero value one value to value this time we have keys in dead of numbers okay cool so let me make a second associative array I'm going to call this ninjas two and set this equal to this time the other way of creating arrays which is the array function and we do it exactly the same way inside so the key first of all then the arrow and then the value so bowser is going to be green a green belt and then it will do peach and peach is going to be yellow okay so that's our second associative array now if we want to print this we can do it exactly the same way prints are and we'll pass in ninjas to like so save it and preview in a browser and now we can see this one as well so we have Bowser which is green and peach which is yellow okay cool so let's comment those out for now and down here and if we wanted to add a new value to one of these things then we can do that by just adding in the new key for example I could say ninjas - I want to add to this array right here square brackets I'm going to make up a key name I'm going to say toad and I'm going to set that equal to pink so what that is going to do is add on here essentially toad pink okay so we're adding on that new field that new value so let's get rid of that and let's print this out now so print underscore R and ninjas to save it and I really fresh over here and now we can see toad is inside the array likewise if we wanted to override something we could say peach the value of the key yeah I'm overriding that with pink so now this will be pink instead of yellow so save that preview again and this time we see that peach is pink etc okay cool so we can also count the amount of elements or values inside associative arrays by using the count function that we also used before so we pass in the array we want to count the elements inside of source a ninja's one and that should be three right but we have one two three elements inside there even though there's six strings each one of these is just one element so let's save that preview and we see three cool okay so we can also merge these associative arrays much like we did this thing up here I'm going to just copy that and I'm going to paste it down here and this time I'm going to say ninjas 3 is equal to a rate merge that's the function name and it's going to be equal to ninjas 1 merged with ninjas too so let's replace those and finally let's print this to the screen who's in print are and ninjas 3 because that's the name of this variable and that should be the merged arrays together here so let's preview that and now we can see we have this big array Shaun Mario Luigi Bowser and Peach okay so there we've got my friends that is basically in a nutshell indexed arrays and associative arrays we will be using these quite frequently as we go forward and when we make our project later on so don't worry if you don't understand it 100% just yet we'll be repeating this many times but in the next video I want to talk about the third type of a rate in PHP which is a multi-dimensional array
Info
Channel: The Net Ninja
Views: 69,747
Rating: undefined out of 5
Keywords: php, tutorial, php tutorial, php tutorial for beginners, mysql, mysql tutorial, mysql tutorial for beginners, sql, sql tutorial, php for beginners, learn php, php mysql, php and mysql, arrays, php array, php arrays, associative array, associative arrays, indexed array, indexed arrays, php associative aray, php indexed array, array tutorial
Id: bWygRxrlD44
Channel Id: undefined
Length: 16min 57sec (1017 seconds)
Published: Mon Feb 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.