'Nested Loop For loop with two inner for loop in PHP

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.



Solution 1:[1]

You can try this:

<?php
createMatrix(40);

function createMatrix($jMax)
{
    $jVal = 0;

    while ($jVal < $jMax) {

        // print line $i
        echo ('$i' . "\t");
        for ($iVal = 1; $iVal <= 12; $iVal++) {
            echo "$iVal\t";
        }
        echo "\n";

        // print line $j
        echo ('$j' . "\t");
        for ($j = 1; $j <= 12; $j++) {
            $jVal++;
            echo "$jVal\t";
            if ($jVal === $jMax) {
                echo "\n";
                break;
            }
        }
        echo "\n";
    }
}

OUTPUT:

Screenshot

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SiL3NC3