PHP Multidimensional Arrays
In this page:
$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
$users = [
["name" => "Alice", "age" => 30],
["name" => "Bob", "age" => 25],
];
// Print a human-readable dump of `$users`
print_r($users);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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'];
?>
Login to try C/C++/Java/PHP code in the editor
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
$rows = [
["id" => 1, "name" => "Alice"],
["id" => 2, "name" => "Bob"],
];
$rows[0]["name"] = "Alicia";
// Print a human-readable dump of `$rows`
print_r($rows);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- Mixing up the order of indexes, such as
$matrix[$col][$row]instead of$matrix[$row][$col]. - Looping with a single
foreachand expecting to reach the inner values, when a nested loop is needed. - Accessing an inner key that does not exist and getting an undefined key warning.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: