0
0
PHPprogramming~10 mins

Multidimensional arrays in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multidimensional arrays
Create outer array
Add inner arrays as elements
Access elements by multiple indexes
Use nested loops to traverse
Process or print each element
Start by creating an array that holds other arrays. Access elements using two or more indexes. Use nested loops to go through all elements.
Execution Sample
PHP
<?php
$matrix = [
  [1, 2],
  [3, 4]
];
echo $matrix[0][1];
?>
This code creates a 2x2 array and prints the element in the first row, second column.
Execution Table
StepActionVariable StateOutput
1Create outer array with two inner arrays$matrix = [[1,2],[3,4]]
2Access $matrix[0]Returns [1, 2]
3Access $matrix[0][1]Returns 2
4Print value 22
5End of script
💡 Script ends after printing the element at $matrix[0][1]
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$matrixundefined[[1,2],[3,4]][[1,2],[3,4]][[1,2],[3,4]][[1,2],[3,4]]
$matrix[0]undefinedundefined[1,2][1,2][1,2]
$matrix[0][1]undefinedundefinedundefined22
Key Moments - 3 Insights
Why do we use two indexes like $matrix[0][1] to access elements?
Because $matrix is an array of arrays. The first index selects the inner array, the second index selects the element inside that inner array. See execution_table rows 2 and 3.
What happens if we try to access $matrix[2][0]?
Since $matrix has only two inner arrays (indexes 0 and 1), $matrix[2] does not exist. This will cause an error or undefined behavior. The execution_table shows valid indexes only.
How do nested loops help with multidimensional arrays?
Nested loops let us go through each inner array and then each element inside it, covering all elements. This is implied in the concept_flow step 'Use nested loops to traverse'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $matrix[0] at step 2?
A2
B[1, 2]
C[3, 4]
Dundefined
💡 Hint
Check the 'Variable State' column at step 2 in execution_table.
At which step does the code output the number 2?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Output' column in execution_table to find when '2' is printed.
If we add another inner array [5,6] to $matrix, how would the variable tracker change after step 1?
A$matrix would be [[1,2],[3,4],[5,6]]
B$matrix would be [[1,2],[3,4]] only
C$matrix would be [5,6]
D$matrix would be undefined
💡 Hint
Refer to the 'After Step 1' column in variable_tracker and imagine adding one more inner array.
Concept Snapshot
Multidimensional arrays are arrays inside arrays.
Access elements with multiple indexes like $array[row][col].
Use nested loops to visit all elements.
Useful for grids, tables, or matrices.
In PHP, arrays can hold arrays easily.
Remember indexes start at 0.
Full Transcript
This example shows how to create and use multidimensional arrays in PHP. We start by making an array that contains other arrays. Each inner array holds numbers. To get a number, we use two indexes: the first picks the inner array, the second picks the number inside it. The code prints the number 2 from the first inner array's second element. Variables keep their values until the script ends. Beginners often wonder why two indexes are needed or what happens if an index is missing. Nested loops help to go through all elements one by one. This is a simple way to handle tables or grids in code.