PHP Multidimensional Arrays: 2D Matrices, Data Modeling & Nested Traversal

In complex web applications, real-world data rarely exists in flat, one-dimensional lists. Think of a college student database: the college has multiple departments, each department contains multiple classes, and each class contains multiple students with multiple grades. A Multidimensional Array is an array that contains one or more nested arrays within its elements, allowing you to model 2D grids, tables, and hierarchical data models.

Two-Dimensional (2D) Coordinate Arrays

A 2D array can be visualized like a spreadsheet table consisting of rows and columns:

<?php
    // A 3x3 Numeric Matrix
    $matrix = [
        [1, 2, 3], // Row 0
        [4, 5, 6], // Row 1
        [7, 8, 9]  // Row 2
    ];

    // Access syntax: $matrix[row][col]
    echo "Center element (Row 1, Col 1): " . $matrix[1][1] . "<br>"; // 5
    echo "Top-right element (Row 0, Col 2): " . $matrix[0][2] . "<br>"; // 3
?>

Nested Associative Arrays (Database Records Model)

In professional web development, multidimensional associative arrays represent database rows fetched from SQL tables:

<?php
    $studentRoster = [
        [
            "id"     => 101,
            "name"   => "Aakash Patel",
            "course" => "BCA",
            "marks"  => ["PHP" => 92, "Java" => 88, "DBMS" => 95]
        ],
        [
            "id"     => 102,
            "name"   => "Simran Kaur",
            "course" => "B.Tech CS",
            "marks"  => ["PHP" => 89, "Java" => 94, "DBMS" => 90]
        ]
    ];

    // Accessing deep nested elements:
    echo "Student: " . $studentRoster[0]["name"] . "<br>";
    echo "Aakash's PHP Mark: " . $studentRoster[0]["marks"]["PHP"] . "<br>";
?>

Traversing Multidimensional Arrays with Nested Loops

To display all records dynamically in an HTML table, use nested foreach loops:

<?php
    $products = [
        ["code" => "P101", "name" => "Laptop",  "stock" => 15, "price" => 55000],
        ["code" => "P102", "name" => "Mouse",   "stock" => 80, "price" => 650],
        ["code" => "P103", "name" => "Monitor", "stock" => 25, "price" => 12000]
    ];

    echo "<table border='1' cellpadding='8' style='border-collapse:collapse;'>";
    echo "<tr style='background:#4F5D95; color:#fff;'><th>Code</th><th>Product Name</th><th>Stock</th><th>Price</th></tr>";

    foreach ($products as $row) {
        echo "<tr>";
        echo "<td>" . $row['code'] . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['stock'] . "</td>";
        echo "<td>₹" . number_format($row['price']) . "</td>";
        echo "</tr>";
    }
    echo "</table>";
?>

BCA / CS University Exam Corner

High-Frequency Exam Question:

Q: How do you extract all values of a single column from a multidimensional array?
A: Use PHP's built-in array_column() function:
$names = array_column($products, 'name'); // Outputs: ['Laptop', 'Mouse', 'Monitor'].

Interactive Student Practice Session

Exercise 24.1: Write a script that performs Matrix Addition on two 2x2 matrices using nested for loops.

Exercise 24.2: Create a multidimensional array representing 3 departments, each with 3 employees. Compute the total payroll expenditure across the company.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!