A PHP multidimensional array is an array whose elements are themselves arrays, which lets you store tables, records and nested data in one variable. You read a value by chaining keys, such as $students[1]['name'], with one pair of brackets per level. Each level can be indexed or associative, and a mix of both is very common.
Typical php multidimensional array uses are database result rows, matrices, grouped data and decoded JSON.
The simplest case is a two-dimensional array, a list of rows.
Arrays of associative arrays represent records and are the most common form in real applications.
Chain the keys to read, assign or append at any level.
Reading a path that may not exist is safe with the null coalescing operator, which works through every level.
Without ??, reading $students[5]['marks']['math'] raises "Undefined array key" warnings in PHP 8.
Use one foreach per dimension.
For records, you can print a small table.
The last row has an empty marks array, so its sum is 0. The totals shown assume the original data from the first example, before the edits in the previous section.
array_column() pulls one column from every row, and can also reindex the result by another column.
Group rows by a field with a loop.
Sort rows by a field with usort() and the spaceship operator.
To swap rows and columns of a matrix, pass the rows to array_map() with null as the callback and spread the matrix.
print_r() prints each inner array over several lines; the content is the same as shown. To count or visit every element at any depth, use count($array, COUNT_RECURSIVE) or array_walk_recursive().
Assigning a nested array copies it by value, so changing the copy does not change the original. A reference left over from foreach is a classic pitfall.
If you skip unset($row), a later foreach ($nums as $row) overwrites the last element. Prefer foreach ($nums as $i => $row) and write to $nums[$i], or use array_map().
| Task | Tool |
|---|---|
| Quick debug view | print_r($a) or var_dump($a) |
| Readable output for the web | echo json_encode($a, JSON_PRETTY_PRINT) |
| Turn JSON into nested arrays | json_decode($json, true) |
| Merge nested config | array_replace_recursive() |
There is no fixed limit. Depth is limited only by memory, though more than three levels usually means you should use objects or classes.
Call count($array, COUNT_RECURSIVE). It counts the nested arrays themselves as well as their elements.
Use isset($a['x']['y']) or $a['x']['y'] ?? $default. Both return safely if any level is missing.
No, but they hold similar data. Arrays of objects or typed classes give you type safety and methods, while nested arrays are faster to write and convert easily to JSON.
A multidimensional array stores arrays inside arrays, accessed with chained keys like $a[1]['name']. Use nested foreach loops to walk it, array_column() to extract fields, usort() to sort rows, ?? to read safely and unset() any reference left by foreach. For complex data, consider classes instead of deep nesting.