Multidimensional arrays help you store data in a table or grid form, like rows and columns. They let you organize related data inside other arrays.
Multidimensional arrays in PHP
<?php // Define a 2D array (array of arrays) $matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; // Access element in row 2, column 3 $value = $matrix[1][2]; ?>
Each element of the main array is itself an array.
Use two sets of square brackets to access elements: first for row, second for column.
<?php // Empty multidimensional array $empty = []; ?>
<?php // 2D array with one row and one column $single = [[42]]; ?>
<?php // Access first element of second row $colors = [ ['red', 'green'], ['blue', 'yellow'] ]; $firstColorSecondRow = $colors[1][0]; ?>
<?php // 3D array example $box = [ [ [1, 2], [3, 4] ], [ [5, 6], [7, 8] ] ]; ?>
This program creates a 2D array to represent seats in rows. It prints the seating chart, changes one name, then prints the updated chart.
<?php // Create a 2D array representing a small seating chart $seatingChart = [ ['Alice', 'Bob', 'Charlie'], ['David', 'Eva', 'Frank'], ['Grace', 'Hank', 'Ivy'] ]; // Print original seating chart echo "Original seating chart:\n"; foreach ($seatingChart as $rowIndex => $row) { echo "Row " . ($rowIndex + 1) . ": "; foreach ($row as $seat) { echo $seat . " "; } echo "\n"; } // Change the seat of 'Eva' to 'Eve' $seatingChart[1][1] = 'Eve'; // Print updated seating chart echo "\nUpdated seating chart:\n"; foreach ($seatingChart as $rowIndex => $row) { echo "Row " . ($rowIndex + 1) . ": "; foreach ($row as $seat) { echo $seat . " "; } echo "\n"; } ?>
Accessing elements is fast, O(1) time, because you use direct indexes.
Multidimensional arrays use more memory because they store arrays inside arrays.
Common mistake: forgetting that each element is an array, so you must use multiple brackets to access nested data.
Use multidimensional arrays when you need to group related data in rows and columns. For simple lists, use one-dimensional arrays.
Multidimensional arrays store data in nested arrays, like tables or grids.
Use multiple square brackets to access or change elements.
They help organize complex data clearly and access it quickly.
Practice
Solution
Step 1: Understand array nesting
A multidimensional array is an array that contains other arrays as its elements.Step 2: Identify the correct definition
An array containing one or more arrays inside it correctly describes this nesting, while others describe unrelated properties.Final Answer:
An array containing one or more arrays inside it -> Option AQuick Check:
Multidimensional array = nested arrays [OK]
- Confusing multidimensional arrays with simple arrays
- Thinking keys must be strings only
- Believing arrays cannot be changed
$colors = [['red', 'green'], ['blue', 'yellow']];
Solution
Step 1: Identify the position of 'blue'
'blue' is in the second inner array (index 1) and is the first element (index 0) there.Step 2: Use correct indices to access
Accessing $colors[1][0] retrieves 'blue'. Other options point to wrong indices or elements.Final Answer:
$colors[1][0] -> Option AQuick Check:
Second array, first element = 'blue' [OK]
- Using wrong index numbers
- Confusing first and second arrays
- Trying to access non-existing elements
$matrix = [[1, 2], [3, 4]];
echo $matrix[0][1] + $matrix[1][0];
Solution
Step 1: Identify the values at given indices
$matrix[0][1] is 2 and $matrix[1][0] is 3.Step 2: Calculate the sum
2 + 3 equals 5, so the output is 5.Final Answer:
5 -> Option CQuick Check:
2 + 3 = 5 [OK]
- Mixing up indices
- Adding wrong elements
- Expecting string output
$arr = [[1, 2], [3, 4];
echo $arr[1][1];
Solution
Step 1: Check array syntax
The array starts with [[ but only one closing bracket ] is present before the semicolon.Step 2: Identify missing bracket
The outer array requires a closing bracket ], so the code should end with ]];Final Answer:
Missing closing bracket for the outer array -> Option BQuick Check:
Outer array brackets must match [OK]
- Confusing inner and outer brackets
- Ignoring syntax errors
- Assuming semicolon fixes bracket issues
<pre>$students = [
['name' => 'Alice', 'scores' => [85, 90]],
['name' => 'Bob', 'scores' => [78, 82]],
['name' => 'Charlie', 'scores' => [92, 88]]
];
Which code correctly calculates and prints the average score of Bob?
Solution
Step 1: Locate Bob's data in the array
Bob is at index 1, and his scores are under the key 'scores'.Step 2: Calculate average correctly
Use array_sum and count on $students[1]['scores'] to get average. $avg = array_sum($students[1]['scores']) / count($students[1]['scores']);
echo $avg; does this correctly.Final Answer:
$avg = array_sum($students[1]['scores']) / count($students[1]['scores']); echo $avg; -> Option DQuick Check:
Index 1 and 'scores' key used correctly [OK]
- Using string key for outer array
- Misspelling 'scores' key
- Using wrong index for Bob
Ready to prove it?
Pass the test and this pattern moves unknown
