PHP Tutorial (& MySQL) #8 - Multidimensional Arrays
Key Takeaways
PHP multidimensional arrays and MySQL integration basics
Full Transcript
okay then my friends so the third type of array I'd like to talk about are 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 arrays okay so the arrays we've created so far have been onedimensional they've had one layer in them this time it's 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 blogs right so let's call this variable blogs 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 Laur Ipson 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 blog so this time it could be called Mario Kart cheats and then the second value would be the author which is Toad and then the third one the content which could be just laurum 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 LM and the number of likes would be 50 okay so now we have these different arrays as elements inside this wrap 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 contrl Zed 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 printor R and then blogs oops blogs like so and that is going to print this array to the browser so if we refresh over here now we can see this 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 01 so we use one 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 one to get this array then we use this index as well on the end which is 01 again so we'd say one on the end of that does that make sense we're accessing the one index for the blog itself this array then this index for the value inside that array so that should print out toad to the screen Okay 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 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 nothing's saying that we're just using indexes 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 laurum and then finally we'll 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 control 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 blog like 0 1 and two 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 going to Echo this string right here link to the browser so let's preview that and now we can see 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 um 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 blogs 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 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 going to be Peach and then the content is just going to be laurum and then finally we have the likes and that's going to be 100 Everyone likes a party okay then so we've added now this new element onto the blogs so it should sit in the next position on the blogs array let's just print R to preview that in the browser so let's save and 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 array oh 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 blogs the last blog in the array then I use a method let me get rid of this method called array pop now this array pop method takes in a parameter or an argument called blogs and what it does is take the last element in inside the array and it pops it off now this returns a value to us right functions 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 uh 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 everything we need for now to do with arrays so in the next video I'd like to start to talk about loops
Original Description
Hey gang, in this PHP tutorial we'll talk about a third kind of array - a multidimensional array, which is essentially just an array of arrays.
----------------------------------------
🐱💻 🐱💻 Course Links:
+ Course files - https://github.com/iamshaunjp/php-mysql-tutorial
+ VS Code editor - https://code.visualstudio.com/
+ Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff
🤑🤑 Donate
+ https://www.paypal.me/thenetninja
🎓🎓 Find me on Udemy
+ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
Cache-Control vs. ETag: What Each One Actually Controls
Dev.to · Susumu Takahashi
JavaScript Parameters, Arguments, Default & Rest Parameters
Dev.to · Megalraja
Automated Testing in Backend Development: A Complete Guide from Unit Tests to Fuzz Testing
Dev.to · Mehrad Sadeghi
I Missed @Service in Node.js, So I Built It with Express
Dev.to · Mike Jung
🎓
Tutor Explanation
DeepCamp AI