Multidimensional Arrays

Introduction

A multidimensional array in PHP is an array that contains one or more arrays inside it. These arrays are useful for storing complex data such as tables, records, student information, product lists, and database results.

Multidimensional arrays help organize related data in a structured format, making it easier to manage and process large amounts of information.

Objectives

By the end of this training, you will be able to:

  • Understand multidimensional arrays in PHP
  • Create and access multidimensional arrays
  • Store structured data efficiently
  • Use loops with multidimensional arrays
  • Display data from nested arrays
  • Apply multidimensional arrays in real projects

What is a Multidimensional Array

A multidimensional array is an array containing one or more arrays.

Example Structure:

<?php
$students = array(
array("Ali", 20, "A"),
array("Sara", 22, "B"),
array("Ahmed", 21, "A")
);
?>

In this example:

  • Each inner array represents one student
  • The outer array stores all student records

Types of Multidimensional Arrays

Two-Dimensional Array

A two-dimensional array stores data in rows and columns.

Example:

<?php
$marks = array(
array(80, 75, 90),
array(70, 85, 88),
array(95, 92, 89)
);
?>

Three-Dimensional Array

A three-dimensional array contains arrays inside arrays inside another array.

Example:

<?php
$data = array(
"ClassA" => array(
"Ali" => array(80, 90),
"Sara" => array(85, 95)
)
);
?>

Accessing Multidimensional Array Values

You can access values using multiple index numbers.

Example:

<?php
$students = array(
array("Ali", 20),
array("Sara", 22)
);

echo $students[0][0];
?>

Output:

Ali

Updating Array Values

You can modify specific values inside a multidimensional array.

Example:

<?php
$students[1][0] = "Ayesha";

echo $students[1][0];
?>

Looping Through Multidimensional Arrays

Using Nested For Loop

Example:

<?php
$numbers = array(
array(1, 2, 3),
array(4, 5, 6)
);

for ($row = 0; $row < 2; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $numbers[$row][$col];
}
}
?>

Using Foreach Loop

Example:

<?php
$students = array(
array("Ali", 20),
array("Sara", 22)
);

foreach ($students as $student) {
echo $student[0];
echo $student[1];
}
?>

Associative Multidimensional Arrays

Associative arrays use named keys instead of index numbers.

Example:

<?php
$employees = array(
array(
"name" => "Ali",
"age" => 25,
"department" => "IT"
),
array(
"name" => "Sara",
"age" => 28,
"department" => "HR"
)
);

echo $employees[0]["name"];
?>

Real World Applications

Multidimensional arrays are used in:

  • Student management systems
  • Employee records
  • Product catalogs
  • Shopping carts
  • Database result storage
  • Report generation systems

Advantages of Multidimensional Arrays

  • Organizes complex data efficiently
  • Easy to group related information
  • Supports table-like structures
  • Useful for dynamic web applications
  • Simplifies data management

Best Practices

  • Use meaningful variable names
  • Keep array structures organized
  • Use loops for large datasets
  • Combine arrays with functions for better performance
  • Validate data before processing

Common Errors

Undefined Index

Occurs when accessing a non-existing index.

Example:

<?php
echo $students[5][0];
?>

Incorrect Loop Limits

Always ensure loop conditions match the array size.

Practice Exercise

Create a multidimensional array to store:

  • Student Name
  • Age
  • Course
  • Marks

Display all records using loops.

Conclusion

Multidimensional arrays in PHP are powerful tools for storing and managing structured data. They allow developers to handle complex information efficiently and are commonly used in real-world web applications and database-driven systems.

Home Ā» Intermediate PHP > Arrays > Multidimensional Arrays