1. 26
    Create a nested or multi-dimensional array in PHP
    2m 34s

Create a nested or multi-dimensional array in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Storing an array within an array is possible with PHP, and are called "multi-dimensional" arrays.

Instructor: [0:00] The setup works for one post, but we are building a blog. I lacely need to support multiple posts. What's cool about PHP arrays is that you can have arrays within arrays. Above our post variable, let's go ahead and create a posts variable.

[0:18] We will set it equal to an empty array. Just like how we put the title and content each on their own lines in this post array, let's open up this bracket within posts. Then what we can do is take the contents of this post and paste it in as the first value to this post variable.

[0:40] Then we can just remove this post variable. We can see that we now have an array. Then an array within that array. This will allow us to store multiple posts within a single array, and it's called a multi-dimensional array. Let's go ahead and copy this first blog post and make another one.

[1:02] We will change this how to learn PHP text to how to learn my SQL. We will update the related text in the content. You also need to make sure there are trailing commas within each value of this post array.

[1:17] Let's go ahead and scroll down to the output and we will replace this H3 and P tag with a single pre-tag. This outputs all of the text within it in a pre-formatted or monospaced font. This is really useful whenever you want to directly output specific data from a PHP tag.

[1:38] Then within it, we will create a new PHP tag and this will call the var_dump function. The value we will pass this function is the posts array. Note how we aren't using a short PHP echo tag here, we're using a full PHP tag.

[1:57] This is because var_dump automatically executes out any data that is passed to it. When we save and refresh the page, we will see the full output of this array. We will see that the main array has a contents of two items, and that the first item has a key of zero.

[2:19] The second item has a key of one. Each of these array values is also assigned to another array each containing two values. This includes the title and content keys.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today