Welcome to today's session on "Multidimensional Arrays and Their Traversal in PHP". Multidimensional arrays in PHP allow you to store arrays within each index instead of single elements. Think of it as an 'apartment building' with floors (outer array) and apartments on each floor (inner array). Our goal today is to build a solid understanding of these 'apartment buildings' and how to handle them effectively in PHP.
To create a multidimensional array in PHP, we use arrays of arrays. Here is an example demonstrating how to create and work with 2D arrays.
php1<?php 2// Creating a 2D array 3$array = [ 4 [1, 2, 3], 5 [4, 5, 6], 6 [7, 8, 9] 7]; 8 9// Printing the 2D array 10print_r($array); 11?>
All indices in PHP arrays are 0-based. Suppose you want to visit an apartment on the second floor (index 1
) and deliver a package to the first unit (index 0
) in this building. Here's how you can do it:
php1<?php 2$array = [ 3 [1, 2, 3], 4 [4, 5, 6], 5 [7, 8, 9] 6]; 7 8// Accessing an element 9echo $array[1][0]; // Outputs: 4 10?>
By using the indices [1][0]
, you can access the value 4
. The number 1
refers to the second inner array, while 0
refers to the first element in that array.
We can visit every floor (outer array) and every apartment on each floor (inner array) using nested loops.
php1<?php 2$array = [ 3 ["Apt 101", "Apt 102", "Apt 103"], 4 ["Apt 201", "Exit Floor", "Apt 203"], 5 ["Apt 301", "Apt 302", "Apt 303"] 6]; 7 8// Loop through 2D array 9foreach ($array as $floor) { 10 foreach ($floor as $apartment) { 11 echo $apartment . ", "; 12 } 13 echo "\n"; 14} 15?> 16 17/* 18Prints: 19Apt 101, Apt 102, Apt 103, 20Apt 201, Exit Floor, Apt 203, 21Apt 301, Apt 302, Apt 303, 22*/
Continuing with the apartment-building analogy, suppose there is a task to replace the old locker code (the second element in the first array) with a new one. Here's how we can do this:
php1<?php 2// Defining and initializing the array 3$array = [ 4 [1, 2, 3], 5 [4, 5, 6], 6 [7, 8, 9] 7]; 8 9// Updating an element 10$array[0][1] = 10; 11 12foreach ($array as $floor) { 13 foreach ($floor as $unit) { 14 echo $unit . " "; 15 } 16 echo "\n"; 17} 18// Output: 191 10 3 204 5 6 217 8 9 22?>
PHP offers simple functions such as count()
to manage multidimensional arrays and determine the number of rows (floors) and columns (units on each floor):
php1<?php 2$array = [ 3 [1, 2, 3], 4 [4, 5, 6], 5 [7, 8, 9] 6]; 7 8// Finding the number of rows 9$numFloors = count($array); 10echo $numFloors . "\n"; // Outputs: 3 11 12// Finding the number of columns 13$numUnits = count($array[0]); 14echo $numUnits . "\n"; // Outputs: 3 15?>
To add a new row or column to a 2D array in PHP, you can manipulate the array structure dynamically. Here is how to add a new row:
Adding a New Row
php1<?php 2$array = [ 3 [1, 2, 3], 4 [4, 5, 6], 5 [7, 8, 9] 6]; 7 8// Adding a new row to our array 9$array[] = [10, 11, 12]; 10 11foreach ($array as $floor) { 12 foreach ($floor as $unit) { 13 echo $unit . " "; 14 } 15 echo "\n"; 16} 17?>
To remove a row or column, you can rearrange the elements of the array:
Removing a Column
php1<?php 2$array = [ 3 [1, 2, 3], 4 [4, 5, 6], 5 [7, 8, 9] 6]; 7 8// Removing the second column 9$newArray = []; 10foreach ($array as $row) { 11 $newRow = []; 12 foreach ($row as $key => $value) { 13 if ($key !== 1) { 14 $newRow[] = $value; 15 } 16 } 17 $newArray[] = $newRow; 18} 19 20foreach ($newArray as $floor) { 21 foreach ($floor as $unit) { 22 echo $unit . " "; 23 } 24 echo "\n"; 25} 26?>
Sometimes, when we visit every apartment on each floor, we might need to move to the next floor midway. break
can help us exit the current loop, while continue
allows us to skip the current iteration and move to the next one.
php1<?php 2$array = [ 3 ["Apt 101", "Apt 102", "Apt 103"], 4 ["Apt 201", "Exit Floor", "Apt 203"], 5 ["Apt 301", "Apt 302", "Apt 303"] 6]; 7 8// Break in nested loop 9foreach ($array as $floor) { 10 foreach ($floor as $apartment) { 11 if ($apartment === "Exit Floor") { 12 break; 13 } 14 echo $apartment . ", "; 15 } 16 echo "\n"; 17} 18?> 19 20/* 21Prints: 22Apt 101, Apt 102, Apt 103, 23Apt 201, 24Apt 301, Apt 302, Apt 303, 25*/
Here, as soon as Exit Floor
is found on a floor, the loop exits, and no further units on that floor are visited. However, all other units are processed as before.
Using continue
in a similar scenario can skip over specific units without leaving the loop:
php1<?php 2$array = [ 3 ["Apt 101", "Apt 102", "Apt 103"], 4 ["Apt 201", "Exit Floor", "Apt 203"], 5 ["Apt 301", "Apt 302", "Apt 303"] 6]; 7 8// Continue in nested loop 9foreach ($array as $floor) { 10 foreach ($floor as $apartment) { 11 if ($apartment === "Exit Floor") { 12 continue; 13 } 14 echo $apartment . ", "; 15 } 16 echo "\n"; 17} 18?> 19 20/* 21Prints: 22Apt 101, Apt 102, Apt 103, 23Apt 201, Apt 203, 24Apt 301, Apt 302, Apt 303, 25*/
In this case, when Exit Floor
is encountered, the continue
statement skips printing Exit Floor
and continues to the next apartment on the floor, meaning the loop does not stop entirely.
Great work! Today we explored various operations on multidimensional arrays, from creating them to updating and traversing them. You also learned how to add and remove rows and columns and how break
and continue
can control flow in nested loops.
Practice solidifies learning! Your next adventure awaits in our upcoming practical exercises, where you'll apply these concepts to multidimensional arrays in PHP. Happy coding!