Multidimensional Arrays in PHP

In my previous post, I discussed about arrays in PHP.

Arrays can contain multiple values at a time. Arrays can also contain other arrays inside them. When you create arrays inside another array, that’s a multidimensional array.

Multidimensional arrays can be two-dimensional, three-dimensional or more. But it gets a little complicated if you create a bigger one.

Let’s check a two-multidimensional array.

The print_r() function will show you the raw information of the array along with the keys you associated.

If you just want the values you can use foreach looping.

The foreach looping is used if you want to output all elements in an array. If you want to output a specific element from an array you can simply use the associative keys.

I have shown both ways in the example below.

You can also change an element of the array by using the keys. Let’s check how we can do that.

Initially the value of ‘Occupation’ key was ‘Students’. Here we changed it to ‘PHP Developer’.

We used the Keys to access the value and change it.

That’s the basics of multidimensional arrays. Let me know through comments if you have any questions.

Click Here to Leave a Comment Below

Leave a Reply: