如何使用循环生成像Excel表这样的行和列键

时间:2022-02-20 20:53:31

I am trying to create loop that gives me this type of result just like excel rows and column.

我正在尝试创建一个循环,它可以给我这种类型的结果,就像excel中的行和列一样。

A1 B1 C1 D1 E1 F1 G1 H1 I1 J1 K1 L1 M1 N1 O1 P1 Q1 R1 S1 T1 U1 V1 W1 X1 Y1 
A2 B2 C2 D2 E2 F2 G2 H2 I2 J2 K2 L2 M2 N2 O2 P2 Q2 R2 S2 T2 U2 V2 W2 X2 Y2
A3 B3 C3 D3 E3 F3 G3 H3 I3 J3 K3 L3 M3 N3 O3 P3 Q3 R3 S3 T3 U3 V3 W3 X3 Y3
A4 B4 C4 D4 E4 F4 G4 H4 I4 J4 K4 L4 M4 N4 O4 P4 Q4 R4 S4 T4 U4 V4 W4 X4 Y4

I have create basic loop but I am confused to create loop.

我已经创建了基本循环,但是我对创建循环感到困惑。

<?php
    for ($char = 'A'; $char <= 'Y'; $char++) {

        echo $char.'2 ';

    }   
?> 

I am not good with complex loop so I thankful If you guide me.

我不擅长复杂的循环,所以我很感激你能引导我。

3 个解决方案

#1


4  

You could use another loop for that row:

你可以用另一个循环来表示这一行:

for($i = 1; $i <= 4; $i++) { // loop for rows
    for($letter = 'A'; $letter != 'Z'; $letter++) { // loop for columns
        echo $letter.$i . ' ';
    }
    echo "<br/>\n";
}

Or tabular:

或表格:

<table border="1">
    <?php for($i = 1; $i <= 4; $i++): ?>
    <tr>
        <?php for($letter = 'A'; $letter != 'Z'; $letter++): ?>
            <td><?php echo $letter.$i; ?></td>
        <?php endfor; ?>
    </tr>
    <?php endfor; ?>
</table>

#2


0  

You should create a string with characters and loop through that string.

您应该创建一个带字符的字符串,并通过该字符串循环。

<?php
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for ($i = 0; $i < strlen($chars); $i++)
    {
        echo $chars[$i] . '2 ';
    }
?>

#3


0  

$letters = range('A', 'Z'); // Generates an array of letters from A-Z
foreach ($n=1; $n<= 10; $n++) { // Loop through numbers from 1-10
    foreach ($letters as $l) { // Loop through the letters
        echo $l.$i; // Print out Letter and number e.g. A1, B1, C1...
    }
    echo "<br>"; // New line after a row of letters
}

#1


4  

You could use another loop for that row:

你可以用另一个循环来表示这一行:

for($i = 1; $i <= 4; $i++) { // loop for rows
    for($letter = 'A'; $letter != 'Z'; $letter++) { // loop for columns
        echo $letter.$i . ' ';
    }
    echo "<br/>\n";
}

Or tabular:

或表格:

<table border="1">
    <?php for($i = 1; $i <= 4; $i++): ?>
    <tr>
        <?php for($letter = 'A'; $letter != 'Z'; $letter++): ?>
            <td><?php echo $letter.$i; ?></td>
        <?php endfor; ?>
    </tr>
    <?php endfor; ?>
</table>

#2


0  

You should create a string with characters and loop through that string.

您应该创建一个带字符的字符串,并通过该字符串循环。

<?php
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for ($i = 0; $i < strlen($chars); $i++)
    {
        echo $chars[$i] . '2 ';
    }
?>

#3


0  

$letters = range('A', 'Z'); // Generates an array of letters from A-Z
foreach ($n=1; $n<= 10; $n++) { // Loop through numbers from 1-10
    foreach ($letters as $l) { // Loop through the letters
        echo $l.$i; // Print out Letter and number e.g. A1, B1, C1...
    }
    echo "<br>"; // New line after a row of letters
}