Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在foreach上的位置_Php_Html - Fatal编程技术网

Php 在foreach上的位置

Php 在foreach上的位置,php,html,Php,Html,如何将位置从0改为1 因为名字以0开头。我所需要做的就是让名字从1开始到50结束 有没有办法解决这个问题 这是我的代码: <html> <head> <title>SEATPLAN</title> </head> <body> <table border = "2" cellpadding = "20" cellspacing = "10">

如何将位置从0改为1

因为名字以0开头。我所需要做的就是让名字从1开始到50结束

有没有办法解决这个问题

这是我的代码:

<html>
<head>
    <title>SEATPLAN</title>
</head>

    <body>
        <table border = "2" cellpadding = "20" cellspacing = "10">                 
            <tr>
                <td colspan = 5 rowspan = 2> </td>
                <td align = "center"> Teachers Table</td>
                <td colspan = 5 rowspan = 2> </td>
            </tr>
            <tr>
                <td colspan = 1 rowspan = 6 width="1000"> </td>
            </tr>

        <?php
                $names = array('Acog','Alaya-ay','Anino','Balsa','Baron','Borda','Bravo','Dalagan','Detumal','Enriquez',    
                                'Hernane','Jose','Laminero','Montilla','Moraclo','Ogang','Palencia','Palencia','Pandili',
                                'Ramo','Ravelo','Septio','Tapel','Tayone','Trinidad','Yntong','Student','Student','Student',
                                'Student','Student','Student','Student','Student','Student','Student','Student','Student',
                                'Student','Student','Student','Student','Student','Student','Student','Student','Student'
                                ,'Student','Student','Student');
            ?>

        <?php
                foreach($names as $position => $name){
                     echo "<td width='500' align='center'>".$position."<br>".$name."<br/>";
                        if ($position == 9){
                            echo "<tr width='500' align='center'>"."<br/>";}
                        if ($position == 19){
                            echo "<tr width='500' align='center'>"."<br/>";}
                        if ($position == 29){
                            echo "<tr width='500' align='center'>"."<br/>";}
                        if ($position == 39){
                            echo "<tr width='500' align='center'>"."<br/>";}
                        }
            ?>

    </table>
</body>
</html>

海图
教师表
我会使用:

$i = 1;
foreach($names as $name) {
     echo '<td>'. $i .': '. $name .'</td>';
     if($i % 10 == 0)
         echo '</tr><tr>';
     $i++;
}
$i=1;
foreach($name作为$name){
回显“.$i.”:“.$name.”;
如果($i%10==0)
回声';
$i++;
}
我会使用:

$i = 1;
foreach($names as $name) {
     echo '<td>'. $i .': '. $name .'</td>';
     if($i % 10 == 0)
         echo '</tr><tr>';
     $i++;
}
$i=1;
foreach($name作为$name){
回显“.$i.”:“.$name.”;
如果($i%10==0)
回声';
$i++;
}

如果您以后想将键(
$position
)用于其他任何操作,这是最简单的解决方案:

echo "<td width='500' align='center'>".($position+1)."<br>".$name."<br/>";
echo”“($position+1)。”
“$name。”
”;
如果您以后想将键(
$position
)用于其他任何操作,这是最简单的解决方案:

echo "<td width='500' align='center'>".($position+1)."<br>".$name."<br/>";
echo”“($position+1)。”
“$name。”
”;
只需添加一个新变量并用
$position+1
对其进行初始化即可

foreach($names as $position => $name){
    $newPosition = $position + 1;
    echo "<td width='500' align='center'>".$newPosition."<br>".$name."<br/>";
    .
    .
}
foreach($name作为$position=>$name){
$newPosition=$position+1;
回显“$newPosition.”
“$name.”
”; . . }

这将同时保留当前
$position
的值。

只需添加一个新变量并用
$position+1
对其进行初始化即可

foreach($names as $position => $name){
    $newPosition = $position + 1;
    echo "<td width='500' align='center'>".$newPosition."<br>".$name."<br/>";
    .
    .
}
foreach($name作为$position=>$name){
$newPosition=$position+1;
回显“$newPosition.”
“$name.”
”; . . }

这将同时保留当前
$position
的值。

所有数字数组都以0作为第一个索引开始

只需执行此技巧,它不会更改阵列结构,但会显示您想要的内容:

foreach($names as $position => $name){
    echo "<td width='500' align='center'>".($position+1)."<br>".$name."<br/>";
    // rest of the code
}
foreach($name作为$position=>$name){
回声“($position+1)。”
“$name。”
”; //代码的其余部分 }

因此,它总是将1添加到实际位置。

所有数字数组都以0作为第一个索引开始

只需执行此技巧,它不会更改阵列结构,但会显示您想要的内容:

foreach($names as $position => $name){
    echo "<td width='500' align='center'>".($position+1)."<br>".$name."<br/>";
    // rest of the code
}
foreach($name作为$position=>$name){
回声“($position+1)。”
“$name。”
”; //代码的其余部分 }

因此,它将始终在实际位置上添加1。

非常感谢您D非常感谢..:D