PHP Tutorial (& MySQL) #8 - Multidimensional Arrays

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay then my friends so the third type of array I'd like to talk about our multi-dimensional arrays and these are pretty easy to get your head around once you've created a couple of them they're basically just arrays within a race okay so the arrays we've created so far have been one-dimensional they've had one layer in them this time is multi-dimensional because we have maybe an array and that array has arrays inside it so each element inside the outer array would be an array itself the best way to understand this is to do an example so let's create one imagine we're storing an array of blocks right so let's call this variable blocks now it wouldn't be enough to just do a single dimensional array for example an indexed array which says blog one and then blog two etc because this is not a blog this is just a string that says blog one a blog has things in it like a title it has an author the content and maybe how many likes it has or something like that so each blog in itself would need to be an array with multiple values inside it that makes sense right so instead let's do this multi-dimensional array to do that so we still have these square brackets for the outer array this time what I'm going to do is enter down so we can place each individual array each element inside this array on its own line so each value this time is an array itself so whereas before it was a string and we'd have maybe another string this time each one of these is going to be an array each item inside the outer array so let's create our first array and we'll just say this has got a title of Mario Party and this is just an indexed array I'm not doing an associative one here the author could be Mario and then the content could be just any kind of lorem ipsum for now and say we have likes as well and this has 30 likes so that's the first item the first value inside this array right here okay so let's do another value another array for another block so this time it could be called mario karts cheats and then the second value would be the author which is told and then the third one the content which could be just larom for now and then finally how many likes 25 okay so the third item could be another blog this time the title could be Zelda hidden chests and then this would be by link and then the content would be just lorem and the number of likes would be 50 okay so now we have these different arrays as elements inside this a wrapper array it's a multi-dimensional array isn't it and both of these arrays they're both indexed arrays because none of them have any key value pairs this one doesn't the outer one they just have three values oops control set they just have three values this this and this and the inner arrays each blog is just an indexed array as well because none of these have values right so say for example we wanted to print all of these well we just say print underscore R and then blogs oops blogs like so and that is going to print this array see the browser so if we refresh over here now we can see these gigantic array like so and that works now say for example we just want to print one of these things for example this one right here then we'd say blogs and then because this is an indexed array we want the second element which is 0-1 so we use 1 as the index and that is going to print this array right here to the screen so save that preview and now we can see this array now say for example we wanted the author right here then we'd say blogs 1 to get this array then we use this index as well on the end which is 0 1 again so we'd say 1 on the end of that does that make sense well accessing the 1 index for the blog itself this array then this index for the value inside that array so that should print out sold to the screen ok cool so that's how we do that in simplest forms using indexed arrays now to me this doesn't really make much sense in this case scenario each blog right here should have I think a key instead of using an indexed array we should use an associative array because who's to say that this is likes who's to say that this is the author etc and nothing saying that we're just using index is right here to grab them and that doesn't really make much sense to another developer so instead of this let me turn these into associative arrays so for example the title here would be Mario Party and the author would be the key for this value and that would be Mario and then the content so let's do a key for that is equal to lorem and then finally it will just say these are likes like so and that is 30 okay that makes a little bit more sense to me because now these have keys and we know what each value is represented by so let's do the same on these two things I'm just going to control click to get my cursor in both places and create a title first of all and do an arrow and then for the second one over here control click again this time it's the author so we'll say author close that off arrow like so and let's just get rid of that extra quotation and then we have the content so let's do those keys as well like so and then finally we have the likes oops so ctrl click like so okay so now we have these associative arrays instead stored inside an index array this is still an indexed array because we're still going to be using indexes to get each individual block like 0 1 & 2 but now inside those different arrays we're using key value pairs and associative array for each one so this time we could say echo and we're going to echo blogs and this time we'll say number two which is this thing right here and say for example we want the author this time we use square brackets and use the key which is author and that makes much more sense so that is now gonna echo this string right here link to the browser so let's preview that and now we can see the link that makes more sense right cool okay so we can still count stuff in here so let me comment this out and say we want to count the number of blogs that we have we could say echo and then count and we want to count the blogs like so save that and preview and now we can see we have three blogs so that still works we can also add new items to this blogs array if we wanted to we just say oops let's do a double slash for that we'd say blocks and then remember we use an empty square bracket to say we want to add something to the end of it so we're going to add a new associative array right here so this time the title is going to be for this new blog Castle party and then the second element the author is gonna be peach and then the content is just gonna be lorem and then finally we have two likes and that's going to be 100 everyone likes a party okay then so we've added now this new element onto the blog's so it should sit in the next position on the blog's array let's just print our to preview that in the browser so let's save and our refresh and now we can see the end one over here is castle party by peach and it has 100 likes cool so there we go my friends that's how we create multi-dimensional arrays on by the way I want to show you one more thing how to take something off an array because I don't think I've shown you that yet we can do this in all types of arrays indexed associative or multi-dimensional so say I want to take off one of the blocks the last blog in the array then I use a method let me get rid of this method called array pop now this array underscore pop method takes in a parameter or an argument call blocks and what it does is take the last element inside that array and it pops it off now this returns a value to us right function generally return a value a result and we can store that result inside a variable I'm going to call this popped and this should be the last array the last element inside this array inside this array that's popped off okay so the thing that is removed is now stored in this variable so if I print underscore R and we want the popped value because that's going to be the array the last one that's popped off which should be this thing right here because that's the one that we added on to the end then save it over here preview we see now that is what has been popped off the array okay cool anyway that is multi-dimensional arrays again we probably will be using these in the future so if you feel like you need more practice don't worry we'll revisit them but I think now that covers everything we need for now to do with arrays so in the next video I'd like to start to talk about loops
Info
Channel: The Net Ninja
Views: 63,307
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, multidimensional array, php multidimensional array, multidimensional, matrix, 3d array, 3d arrays, php matrix, multidimensional arrays
Id: G1iDSoAXyvM
Channel Id: undefined
Length: 10min 6sec (606 seconds)
Published: Tue Feb 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.