I require a nested for
loop for my php website which gives me results as follows.
I want to two integers incrementally - one stops at 12 and other continues until the specified values.
If one string is $i
and second string is $j
than I want output as:
$i 1 2 3 4 5 6 7 8 9 10 11 12
$j 1 2 3 4 5 6 7 8 9 10 11 12
$i 1 2 3 4 5 6 7 8 9 10 11 12
$j 13 14 15 16 17 18 19 20 21 22 23 24
$i 1 2 3 4 5 6 7 8 9 10 11 12
$j 25 26 27 28 29 30 31 32 33 34 35 36
It should be repeated up to n
values.
My Answer:
Here is the code for the nested for loop in PHP:
php
$n = 3; // Number of times to repeat the pattern
for ($k = 0; $k < $n; $k++) {
for ($i = 1; $i <= 36; $i += 12) {
echo "\$i ";
for ($j = $i; $j <= $i + 11; $j++) {
echo $j . " ";
}
echo "
";
echo "\$j ";
for ($j = $i; $j <= $i + 11; $j++) {
echo $j + 12*$k . " ";
}
echo "
";
}
}
?>
This code will output the pattern you described, repeating it for the specified number of times (in this case, 3 times). You can change the value of `$n` to adjust the number of repetitions.
Rate this post
4 of 5 based on 6545 votesComments