Different type of Nested Loops in PHP

In PHP, a nested loop is a loop that is contained within another loop. Nested loops can be useful for performing complex tasks that involve multiple layers of iteration. In this blog post, we will explore the different types of nested loops in PHP and discuss the best way to use them with examples.

There are four main types of loops in PHP: for loops, while loops, do-while loops, and foreach loops. Any of these loops can be nested within another loop of the same type or a different type.

For Loop Nesting

To nest a for loop within another for loop, you can use the following syntax:

for (initialization1; condition1; increment1) {
  for (initialization2; condition2; increment2) {
    // code to be executed
  }
}

Here is an example of a nested for loop in PHP that prints a multiplication table:

<!-- wp:code -->
<pre class="wp-block-code"><code>for (initialization1; condition1; increment1) {
  for (initialization2; condition2; increment2) {
    // code to be executed
  }
}</code></pre>
<!-- /wp:code -->

While Loop Nesting

To nest a while loop within another while loop, you can use the following syntax:

while (condition1) {
  while (condition2) {
    // code to be executed
  }
}

Here is an example of a nested while loop in PHP that prints the Fibonacci sequence:

$a = 0;
$b = 1;
while ($b < 100) {
  while ($b < 100) {
    echo "$a\n";
    $c = $a + $b;
    $a = $b;
    $b = $c;
  }
}

Do-While Loop Nesting

To nest a do-while loop within another do-while loop, you can use the following syntax:

do {
  do {
    // code to be executed
  } while (condition2);
} while (condition1);

Here is an example of a nested do-while loop in PHP that prints the numbers 1 through 10 in a pyramid shape:

$i = 1;
do {
  $j = 1;
  do {
    echo $j . " ";
    $j++;
  } while ($j <= $i);
  echo "\n";
  $i++;
} while ($i <= 10);

Foreach Loop Nesting

To nest a foreach loop within another foreach loop, you can use the following syntax:

foreach ($array1 as $key1 => $value1) {
  foreach ($array2 as $key2 => $value2) {
    // code to be executed
  }
}

Here is an example of a nested foreach loop in PHP that prints the keys and values of two associative arrays:

$fruits = [
    "apple" => "A round fruit with a red or green skin.",
    "banana" => "A long, curved fruit with a yellow skin.",
    "orange" => "A round fruit with a orange skin and a sweet, citrus flavor.",
];

$prices = [
    "apple" => 1.50,
    "banana" => 0.99,
    "orange" => 2.00,
];

foreach ($fruits as $fruit => $description) {
    foreach ($prices as $priceFruit => $price) {
        if ($fruit == $priceFruit) {
            echo "$fruit: $description, $price\n";
        }
    }
}

This code will output the following:

apple: A round fruit with a red or green skin., 1.5
banana: A long, curved fruit with a yellow skin., 0.99
orange: A round fruit with a orange skin and a sweet, citrus flavor., 2

The nested foreach loop iterates over both arrays and compares the keys to find matching fruit names. If a match is found, it prints the fruit name, description, and price. This allows you to combine the data from both arrays and print it in a single loop.