← Back to PHP Course | Chapter 7: Arrays | Lesson 9 of 13

PHP Multidimensional Arrays

A multidimensional array is like a chest of drawers where each drawer holds its own smaller box of things. It lets you keep tables, such as students and their grades, in one place.
Syntax
php
$array = [
    ["key" => value, "key2" => value2],
    ["key" => value, "key2" => value2],
];

$array[0]["key"];             // read nested value
$array[1]["key2"] = new_value;

Introduction to Multidimensional Arrays

A multidimensional array is simply an array whose values are themselves arrays, letting you model grid-like or nested data such as a spreadsheet of rows and columns, or a list of users each with their own set of properties.

Example: Introduction to Multidimensional Arrays

php
<?php
$users = [
    ["name" => "Alice", "age" => 30],
    ["name" => "Bob", "age" => 25],
];
// Print a human-readable dump of `$users`
print_r($users);
?>

Accessing Nested Elements

You access a nested value by chaining index brackets, like $matrix[1][2] for a 2D grid or $users[0][email] for the email of the first user in a list of associative arrays.

Example: Accessing Nested Elements

php
<?php
// Declare `$matrix` as an array: `[[1, 2], [3, 4]]`
$matrix = [[1, 2], [3, 4]];
// Print `$matrix[1][0] . "\n"` to the output
echo $matrix[1][0] . "\n";

// Declare `$users` as an array: `[["email" => "[email protected]"]]`
$users = [["email" => "[email protected]"]];
// Print `$users[0]['email']` to the output
echo $users[0]['email'];
?>

Modifying Multidimensional Arrays

A common real-world shape is an array of associative arrays — for example, database query results are typically returned this way, with each outer element representing one row and its inner keys representing columns.

Example: Modifying Multidimensional Arrays

php
<?php
$rows = [
    ["id" => 1, "name" => "Alice"],
    ["id" => 2, "name" => "Bob"],
];
$rows[0]["name"] = "Alicia";
// Print a human-readable dump of `$rows`
print_r($rows);
?>

Looping Through Multidimensional Arrays

Nested foreach loops are the standard way to walk a multidimensional array: an outer loop for each row and an inner loop for each column, which is exactly how you'd render an HTML table from tabular data.

Example: Looping Through Multidimensional Arrays

php
<?php
// Declare `$grid` as an array: `[[1, 2], [3, 4]]`
$grid = [[1, 2], [3, 4]];
// Loop over `$grid`, binding each item to `$row`
foreach ($grid as $row) {
    // Loop over `$row`, binding each item to `$cell`
    foreach ($row as $cell) {
        // Print `$cell . " "` to the output
        echo $cell . " ";
    }
    // Print "\n" to the output
    echo "\n";
}
?>

Multi-level Arrays

print_r() or var_dump() are invaluable for debugging multidimensional arrays, since they reveal the full nested structure and types at a glance instead of you having to guess how deep the nesting goes.

Example: Multi-level Arrays

php
<?php
// Declare `$data` as an array: `["user" => ["name" => "Alice", "roles" => ["admin", "editor"]]]`
$data = ["user" => ["name" => "Alice", "roles" => ["admin", "editor"]]];
// Print a human-readable dump of `$data`
print_r($data);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Mixing up the order of indexes, such as $matrix[$col][$row] instead of $matrix[$row][$col].
  2. Looping with a single foreach and expecting to reach the inner values, when a nested loop is needed.
  3. Accessing an inner key that does not exist and getting an undefined key warning.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.