PHP Multidimensional Arrays

11/22/2025

PHP multidimensional arrays explained with examples

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


PHP multidimensional arrays explained with examples

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" => "amit@example.com",
        "city" => "Delhi"
    ],
    [
        "name" => "Priya",
        "email" => "priya@example.com",
        "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.

Table of content

  • Introduction to Apache Hive
  • Hive Architecture and Components
  • Hive Modes
  • Installation and Setup
    • Installing Hive on Linux/Windows
    • Configuring Hive with Hadoop
    • Verifying the Installation
  • Working with Hive Tables
    • Internal (Managed) Tables
    • External Tables
    • Creating Tables in Hive
    • Altering and Dropping Tables
    • Partitioning in Hive
    • Bucketing in Hive
  • HiveQL Basics
    • SELECT Queries
    • Filtering Data with WHERE
    • Sorting and Grouping Data
    • Using Joins in Hive
  • Advanced Hive Concepts
    • Partition Pruning
    • Dynamic Partitioning
    • Query Optimization in Hive
    • Working with Hive Indexes
    • ACID Transactions in Hive
  • File Formats in Hive
    • Text File
    • ORC (Optimized Row Columnar)
    • Parquet
    • Avro
    • Sequence File
  • Hive Functions
    • Built-in Functions (String, Date, Math)
    • Aggregate Functions
    • User-Defined Functions (UDFs)
  • Integrating Hive with Other Tools
    • Hive and Apache Spark
    • Hive and Pig
    • Hive and HBase
  • Hive Interview Questions
  • Best Practices in Hive
    • Performance Optimization
    • Handling Large Datasets
    • Security and Access Control
  • FAQs and Common Errors in Hive
    • Troubleshooting Hive Issues
    • Frequently Asked Questions
  • Resources and References
    • Official Hive Documentation
    • Recommended Books and Tutorials