codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

22 / 56 topics
19Arrays in PHP20Indexed Arrays21Associative Arrays22Multidimensional Arrays23Array Functions
Tutorials/PHP/Multidimensional Arrays
🐘PHP

Multidimensional Arrays

Updated 2026-05-15
10 min read

Multidimensional Arrays

Introduction

In PHP, an array is a collection of values that can be accessed by their index. These values can be of any data type, including other arrays. When an array contains one or more arrays as its elements, it is referred to as a multidimensional array. This tutorial will guide you through the concept of multidimensional arrays in PHP, how to create them, access their elements, and perform operations on them.

Concept

A multidimensional array is essentially an array of arrays. The simplest form of a multidimensional array is a two-dimensional array, which can be visualized as a table with rows and columns. Each element in the outer array is itself an array, representing a row in the table.

PHP supports arrays of any dimensionality, from two-dimensional to n-dimensional arrays. However, for practical purposes, we often deal with two-dimensional arrays due to their simplicity and wide usage in various applications like tables, matrices, etc.

Examples

Creating a Two-Dimensional Array

To create a two-dimensional array, you can define an array of arrays. Here's an example:

php
1$matrix = [
2 [1, 2, 3],
3 [4, 5, 6],
4 [7, 8, 9]
5];

In this example, $matrix is a two-dimensional array with three rows and three columns.

Accessing Elements

To access elements in a multidimensional array, you need to specify the indices for each dimension. For instance, to access the element at the second row and third column of the above matrix, you would use:

php
1echo $matrix[1][2]; // Outputs: 6

Iterating Over a Two-Dimensional Array

You can iterate over a two-dimensional array using nested loops. Here's an example that prints all elements of the matrix:

php
1foreach ($matrix as $row) {
2 foreach ($row as $value) {
3 echo $value . ' ';
4 }
5 echo "
6";
7}

The output will be:

1 2 3 
4 5 6 
7 8 9 

Adding Elements

You can add elements to a multidimensional array by specifying the indices. For example, to add a new row to the matrix:

php
1$matrix[] = [10, 11, 12];

Now, the matrix will have four rows.

Removing Elements

To remove an element from a multidimensional array, you can unset it using its indices. For example, to remove the second row:

php
1unset($matrix[1]);

The resulting matrix will be:

[
    [1, 2, 3],
    [7, 8, 9]
]

Example: Creating and Manipulating a Two-Dimensional Array

Here's a complete example that demonstrates creating a two-dimensional array, accessing its elements, iterating over it, adding a new row, and removing an element:

php
1$matrix = [
2 [1, 2, 3],
3 [4, 5, 6],
4 [7, 8, 9]
5];
6
7echo "Original Matrix:
8";
9foreach ($matrix as $row) {
10 foreach ($row as $value) {
11 echo $value . ' ';
12 }
13 echo "
14";
15}
16
17echo "
18Element at row 2, column 3: " . $matrix[1][2] . "
19";
20
21$matrix[] = [10, 11, 12];
22echo "
23Matrix after adding a new row:
24";
25foreach ($matrix as $row) {
26 foreach ($row as $value) {
27 echo $value . ' ';
28 }
29 echo "
30";
31}
32
33unset($matrix[1]);
34echo "
35Matrix after removing the second row:
36";
37foreach ($matrix as $row) {
38 foreach ($row as $value) {
39 echo $value . ' ';
40 }
41 echo "
42";
43}

The output will be:

Original Matrix:
1 2 3 
4 5 6 
7 8 9 

Element at row 2, column 3: 6

Matrix after adding a new row:
1 2 3 
4 5 6 
7 8 9 
10 11 12 

Matrix after removing the second row:
1 2 3 
7 8 9 
10 11 12 

What's Next?

In the next section, we will explore various array functions in PHP that can be used to manipulate and work with arrays efficiently. These functions include sorting, searching, filtering, and more.

Stay tuned for more tutorials on PHP!


PreviousAssociative ArraysNext Array Functions

Recommended Gear

Associative ArraysArray Functions