PHP Multidimensional Arrays

11/22/2025

markdown

Go Back

PHP Multidimensional Arrays: A Complete Beginner-Friendly Guide

Multidimensional arrays are powerful array structures in PHP that allow you to store arrays inside another array. These are useful when you need to represent complex, structured data such as student records, product lists, or database rows.

In this tutorial, you will learn:

  • What multidimensional arrays are

  • Types of multidimensional arrays

  • How to create and access them

  • How to loop through nested arrays

  • Real-world developer examples


markdown

What Is a Multidimensional Array?

A multidimensional array is an array that contains one or more arrays inside it.

Example:

$students = [
    ["Amit", 20, "PHP"],
    ["Sneha", 22, "Java"],
    ["Raj", 21, "Python"]
];

This is a 2-dimensional array (array of arrays).

Each inner array contains data for one student.


Types of Multidimensional Arrays

1. Indexed Multidimensional Array

A simple array of indexed arrays.

$marks = [
    [85, 78, 92],
    [88, 90, 76],
    [79, 82, 84]
];

2. Associative Multidimensional Array

An array containing associative arrays.

$user = [
    [
        "name" => "Amit",
        "email" => "[email protected]",
        "city" => "Delhi"
    ],
    [
        "name" => "Priya",
        "email" => "[email protected]",
        "city" => "Mumbai"
    ]
];

3. Mixed Multidimensional Array

An array that contains both indexed and associative arrays.

$products = [
    "electronics" => [
        ["Laptop", 55000],
        ["Mouse", 799]
    ],
    "clothing" => [
        ["Shirt", 999],
        ["Jeans", 1499]
    ]
];

Accessing Elements in Multidimensional Arrays

You can use multiple indexes to access values.

$students = [
    ["Amit", 20, "PHP"],
    ["Sneha", 22, "Java"]
];

echo $students[0][0]; // Amit
echo $students[1][2]; // Java

Looping Through Multidimensional Arrays

1. Using Nested foreach Loops

$students = [
    ["Amit", 20, "PHP"],
    ["Sneha", 22, "Java"]
];

foreach ($students as $student) {
    foreach ($student as $value) {
        echo $value . " ";
    }
    echo "<br>";
}

2. Using Nested for Loops

$marks = [
    [85, 78, 92],
    [88, 90, 76]
];

for ($i = 0; $i < count($marks); $i++) {
    for ($j = 0; $j < count($marks[$i]); $j++) {
        echo $marks[$i][$j] . " ";
    }
    echo "<br>";
}

Real-World Example: Employee Records

$employees = [
    [
        "id" => 101,
        "name" => "Rahul",
        "department" => "IT",
        "salary" => 45000
    ],
    [
        "id" => 102,
        "name" => "Priya",
        "department" => "HR",
        "salary" => 40000
    ],
    [
        "id" => 103,
        "name" => "Karan",
        "department" => "Finance",
        "salary" => 50000
    ]
];

foreach ($employees as $emp) {
    echo "ID: " . $emp["id"] . " - Name: " . $emp["name"] . " - Department: " . $emp["department"] . "<br>";
}

Useful Functions for Multidimensional Arrays

array_column() – Get a column of values

echo implode(", ", array_column($employees, "name"));

in_array() – Search inside nested arrays

if (in_array("IT", array_column($employees, "department"))) {
    echo "IT Department Found";
}

json_encode() – Convert multidimensional array to JSON

echo json_encode($employees);

When to Use Multidimensional Arrays

Use them when you need to store:

  • Database-like records

  • Product catalogues

  • Student lists with details

  • API data

  • Complex structured information


Conclusion

Multidimensional arrays are a crucial part of PHP development. They allow you to manage large, structured data sets easily. Whether you're handling database results or building API responses, mastering them helps write cleaner and more efficient code.


Meta Description

Learn PHP multidimensional arrays with examples. Understand how to create, access, and loop through nested arrays in PHP.

Meta Keywords

php multidimensional arrays, php nested arrays, php array examples, php tutorial for beginners

Alt Tag

"PHP multidimensional arrays explained with examples"