Menu
Coddy logo textTech

Iterating with Nested Loops

Part of the Logic & Flow section of Coddy's PHP journey — lesson 30 of 68.

When working with 2D arrays, you often need to process every single element, which requires visiting each row and then each column within that row. This is where nested loops become essential—you use one loop inside another to systematically traverse the entire grid structure.

The outer loop iterates through each row of the 2D array, while the inner loop processes each element within the current row. Here's how this works with a foreach loop:

<?php
$schedule = [
    ["Meeting", "Lunch", "Presentation"],
    ["Training", "Review", "Planning"],
    ["Workshop", "Break", "Demo"]
];

foreach ($schedule as $day) {
    foreach ($day as $appointment) {
        echo $appointment . "\n";
    }
}
?>

You can also use traditional for loops when you need more control over the indices. This approach gives you access to both the row and column positions:

<?php
for ($row = 0; $row < count($schedule); $row++) {
    for ($col = 0; $col < count($schedule[$row]); $col++) {
        echo "Day " . ($row + 1) . ", Time " . ($col + 1) . ": " . $schedule[$row][$col] . "\n";
    }
}
?>

This nested loop pattern is fundamental for processing 2D arrays completely, whether you're displaying data, performing calculations, or searching for specific values across the entire grid structure.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You will receive one input: a 2D array representing a classroom seating arrangement in JSON format. Each inner array represents a row of seats, and each element contains either a student's name or an empty string for vacant seats. Read the input, convert the JSON string to a 2D array, use nested loops to iterate through the entire seating arrangement, and print each student's name along with their position in the format: Row X, Seat Y: Name

Skip vacant seats (empty strings) and only print information for occupied seats. Rows and seats should be numbered starting from 1 (not 0).

Input format: One line containing a JSON array representing the seating arrangement (example: [["Alice","Bob",""],["","Charlie","Diana"],["Eve","","Frank"]])

Expected output: Each occupied seat printed on a separate line in the format Row X, Seat Y: Name, where X is the row number and Y is the seat number (both starting from 1)

Cheat sheet

Use nested loops to process every element in a 2D array. The outer loop iterates through rows, while the inner loop processes elements within each row.

Using foreach loops:

<?php
$schedule = [
    ["Meeting", "Lunch", "Presentation"],
    ["Training", "Review", "Planning"],
    ["Workshop", "Break", "Demo"]
];

foreach ($schedule as $day) {
    foreach ($day as $appointment) {
        echo $appointment . "\n";
    }
}
?>

Using traditional for loops for index access:

<?php
for ($row = 0; $row < count($schedule); $row++) {
    for ($col = 0; $col < count($schedule[$row]); $col++) {
        echo "Day " . ($row + 1) . ", Time " . ($col + 1) . ": " . $schedule[$row][$col] . "\n";
    }
}
?>

Try it yourself

<?php
// Read the JSON input
$input = trim(fgets(STDIN));

// Convert JSON string to 2D array
$seating = (array)json_decode($input, true);

// TODO: Write your code below
// Use nested loops to iterate through rows and seats
// Remember to skip empty seats and number rows/seats starting from 1


?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow