Php 将数组转储到表中

Php 将数组转储到表中,php,arrays,Php,Arrays,我有一个数组,我想将其转储到一个表中,但有一个新的列X项目数。例如: 项目1 |项目2 |项目3 |项目4| 项目5 |项目6 |项目7 |项目8| 第9项等等 我已经可以使用以下代码在一定数量的项目(在本例中为4)后添加一个新列: $input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); $cols = 4; echo "<table border=

我有一个数组,我想将其转储到一个表中,但有一个新的列X项目数。例如:

项目1 |项目2 |项目3 |项目4|

项目5 |项目6 |项目7 |项目8|

第9项等等

我已经可以使用以下代码在一定数量的项目(在本例中为4)后添加一个新列:

$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); 

$cols = 4; 

echo "<table border=\"5\" cellpadding=\"10\">"; 

for ($i=0; $i < count($input); $i++) 
{ 
echo "<tr>"; 
    for ($c=0; $c<$cols; $c++) 
    {
    $n = $i+$c;
    echo "<td>$input[$n]</td>"; 
    } 
echo "</tr>"; 
$i += $c; 
} 

echo "</table>"; 
$input=array('1','2','3','4','5','6','7','8','9','10');
$cols=4;
回声“;
对于($i=0;$i对于($c=0;$c数组函数可能非常神奇:

$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); 
$cols = 4; 
$table = array_chunk($input, $cols);

echo "<table border=\"5\" cellpadding=\"10\">";

foreach($table as $row) {
    echo "<tr>"; 
    foreach($row as $cell) {
        echo "<td>$cell</td>"; 
    }
    echo "</tr>"; 
}
echo "</table>";
$input=array('1','2','3','4','5','6','7','8','9','10');
$cols=4;
$table=array\u chunk($input,$cols);
回声“;
foreach($行的表格){
回声“;
foreach($行作为$单元格){
回声“$cell”;
}
回声“;
}
回声“;

参考:

数组函数可能非常神奇:

$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); 
$cols = 4; 
$table = array_chunk($input, $cols);

echo "<table border=\"5\" cellpadding=\"10\">";

foreach($table as $row) {
    echo "<tr>"; 
    foreach($row as $cell) {
        echo "<td>$cell</td>"; 
    }
    echo "</tr>"; 
}
echo "</table>";
$input=array('1','2','3','4','5','6','7','8','9','10');
$cols=4;
$table=array\u chunk($input,$cols);
回声“;
foreach($行的表格){
回声“;
foreach($行作为$单元格){
回声“$cell”;
}
回声“;
}
回声“;
参考:

$input=array('1','2','3','4','5','6','7','8','9','10');
$cols=4;
回声“;
对于($i=0;$i$input=array('1','2','3','4','5','6','7','8','9','10');
$cols=4;
回声“;
对于($i=0;$i对于($c=0;$c您每5次跳过一次,因为当您检测到需要执行新行时,您将使用for循环额外增加计数器时间

以下是另一种方法:

$ary = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8');


echo "<table><tr>";
foreach($ary as $k => $item)
{
    if($k % 4 == 0 && $k != 0)
        echo "</tr><tr>";
    echo "<td>$item</td>";
}
echo "</tr></table>";
$ari=array('item1','item2','item3','item4','item5','item6','item7','item8');
回声“;
foreach($k=>$item)
{
如果($k%4==0&$k!=0)
回声“;
回显“$item”;
}
回声“;

您每5行跳过一次,因为当您检测到需要执行新行时,您将使用for循环额外增加计数器的时间

以下是另一种方法:

$ary = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8');


echo "<table><tr>";
foreach($ary as $k => $item)
{
    if($k % 4 == 0 && $k != 0)
        echo "</tr><tr>";
    echo "<td>$item</td>";
}
echo "</tr></table>";
$ari=array('item1','item2','item3','item4','item5','item6','item7','item8');
回声“;
foreach($k=>$item)
{
如果($k%4==0&$k!=0)
回声“;
回显“$item”;
}
回声“;

在第一个循环中,$i将增加$c(始终为3)。然后for循环将$i值增加1($i++),这将使其跳过“五”


您可以控制增量,或者让for循环为您控制增量。

在第一个循环中,$i将增加$c(始终为3)。然后for循环将$i值增加1($i++),这将使其跳过“五”


您可以控制增量,也可以让for循环为您控制增量。

如果您清楚地理解,您希望转储一个按您指定的行数排列的数组:

for ($i=0; $i < count($input); $i++) 
{ 
echo "<tr>"; 
    for ($c=0; $c<$cols; $c++) 
    {
    echo "<td>$input[$i]</td>"; 
    } 
echo "</tr>"; 
} 
for($i=0;$i对于($c=0;$c,如果您清楚地理解,则希望转储一个数组,其行数由您指定:

for ($i=0; $i < count($input); $i++) 
{ 
echo "<tr>"; 
    for ($c=0; $c<$cols; $c++) 
    {
    echo "<td>$input[$i]</td>"; 
    } 
echo "</tr>"; 
} 
for($i=0;$i对于($c=0;$c如果您希望保留原始逻辑,请将
$i
的增量更改为
$i+=(c$-1)
,因为除了列宽之外,您还在循环中增加
$i
。然后还要考虑空值。这将起作用:

<?php
$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',     'ten'); 

$cols = 4; 

echo "<table border=\"5\" cellpadding=\"10\">" . PHP_EOL; 

for ($i=0; $i < count($input); $i++) 
{ 
echo "<tr>"; 
    for ($c=0; $c<$cols; $c++) 
    {
    $n = $i+$c;
    if ( $n < count($input))
        echo "<td>$input[$n]</td>" . PHP_EOL; 
    else
        echo "<td></td>" . PHP_EOL;
    } 
echo "</tr>"; 
$i += ($c-1); 
} 

echo "</table>"; 
?>

如果您希望保留原始逻辑,请将
$i
的增量更改为
$i+=(c$-1)
,因为您在循环中除了列宽之外还增加了
$i
。然后还要考虑空值。这将起作用:

<?php
$input = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',     'ten'); 

$cols = 4; 

echo "<table border=\"5\" cellpadding=\"10\">" . PHP_EOL; 

for ($i=0; $i < count($input); $i++) 
{ 
echo "<tr>"; 
    for ($c=0; $c<$cols; $c++) 
    {
    $n = $i+$c;
    if ( $n < count($input))
        echo "<td>$input[$n]</td>" . PHP_EOL; 
    else
        echo "<td></td>" . PHP_EOL;
    } 
echo "</tr>"; 
$i += ($c-1); 
} 

echo "</table>"; 
?>


这是因为你在循环中重置
$i
,然后
for()
本身将执行
$i++
。你应该在循环的顶部重置
for($i…;$i+=$c)
或者在循环的底部重置
$i+=$c-1;
,这是因为你在循环中重置
$i
,然后是
for()
本身将执行
$i++
。您应该为($i.;$i+=$c)执行
在顶部或
$i+=$c-1;
在底部,我认为$rows、$row、$cell将导致未定义的变量消息错误。@SIFE我认为他指的是$table而不是$rows。我更改了它,现在它可以正常工作了。@Preston是的,我决定不喜欢$rows,但没有完全这样做,编辑时使用thx。我认为$rows、$row、$cell将导致und定义变量消息错误。@SIFE我想他指的是$table而不是$rows。我更改了它,现在它可以正常工作了。@Preston是的,我决定不喜欢$rows,但没有完全这样做,谢谢编辑。