2012-04-18 82 views
1

循环可以说我有一个数组通过阵列中的多个​​细胞

$aSomeArray = array("1","2","3","4","5","6","7","8","9","10","11","12"); 

在一个表行立即显示一个数组值是容易这样

echo "<table>"; 

foreach ($aSomeArray as $iSomeArrayKey => $iSomeArrayValue) 
{ 
    echo "<tr>"; 
    echo "<td>".$iSomeArrayValue."</td>"; 
    echo "</tr>"; 
} 

echo "</table>"; 

但我想显示的值这样的表格格式

1 2 3 4 
5 6 7 8 
9 10 11 12 

我该如何做到这一点?

+0

你将如何确定在哪个索引处的数组迭代停止?它是否遵循一般的任何模式? – Dhiraj 2012-04-18 11:34:24

+0

没有任何特定的模式,我只想获取数据显示在多个TD而不是一次一个行 – skos 2012-04-18 11:35:52

回答

6

编辑,这个工程:

$aSomeArray = array("1","2","3","4","5","6","7","8","9","10","11","12"); 
$i = 0; 
echo "<table>\r\n"; 
foreach ($aSomeArray as $aSomeArrayKey => $aSomeArrayValue) 
{ 
    if (($i % 4) == 0) echo "\t<tr>\r\n"; 
    echo "\t\t<td>" . $aSomeArrayValue . "</td>\r\n"; 
    if (($i % 4) == 3) echo "\t</tr>\r\n"; 
    $i++; 
} 
echo "</table>\r\n"; 
+0

对不起,但这段代码不起作用 – skos 2012-04-18 12:29:43

+0

它有什么问题?你有错误吗?或者什么是返回的输出?你确定你写得对吗? – 2012-04-18 13:36:32

+0

Ooops。抱歉。我测试了它,改变了它,并且这次它工作。还添加了一些制表符和换行符,这在“查看源代码”中看起来更好。 – 2012-04-18 13:42:17

0
echo "<table>"; 
echo "<tr>"; 
foreach ($aSomeArray as $aSomeArrayKey => $aSomeArrayValue) 
{ 
echo "<td>".$aSomeArrayValue."</td>"; 
if($aSomeArrayValue % 4 == 0) 
    echo "</tr><tr>"; 
} 
echo "</tr>"; 
echo "</table>"; 
1

只是做一个运行计数。因此,在foreach设置$i = 1和每第四个结果之前,将计数重置为$i = 1,然后结束该行并重新打开一个新行。

0
$i=1; 
echo "<tr>"; 
foreach ($aSomeArray as $aSomeArrayKey => $aSomeArrayValue) 
{ 

    echo "<td>".$aSomeArrayValue."</td>"; 
    $i++; 
    if($i%4==0){ 
     echo "<tr></tr>"; 
    } 
} 
echo "</tr>"; 

它也可用于不可分割的4个元素。

1
$aSomeArray = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"); 

$columns = 4; // The number of columns to shown 

echo "<table>"; 

$i = 0; 
$trOpen = false; // just a flag if <tr> has been closed (paired with </tr>) or not. 
foreach ($aSomeArray as $item) { 
    if ($i % $columns == 0) { 
     echo "<tr>"; 
     $trOpen = true; 
    } 

    echo "<td>" . $item . "</td>"; 

    if (($i + 1) % $columns == 0) { 
     echo "</tr>"; 
     $trOpen = false; 
    } 

    $i++; 
} 

if ($trOpen) { 
    echo "</tr>"; // add '</tr>' if it is not yet added. 
} 

echo "</table>"; 
2

只是一个想法,看起来更好恕我直言。

<?php 
$aSomeArray = array("1","2","3","4","5","6","7","8","9","10","11","12"); 

function createMatrix($width, $array) 
{ 
$newArray = array(); 
$temp = array(); 
$count = 1; 

foreach($array as $key => $value) 
{ 
    $temp[] = $value; 

    if(($count++ % $width) == 0) 
    { 
    $newArray[] = $temp; 
    $temp = array(); 
    } 
} 
if(count($temp) > 0) 
{ 
    $newArray[] = $temp; 
} 
return $newArray; 
} 

将创建变量$width 的矩阵阵列然后可以使用该数据作为一个双重的for-each这样的:

$matrix = createMatrix(2, $aSomeArray); 

foreach($matrix as $row) 
{ 
echo "<tr>\n"; 
foreach($row as $td) 
{ 
    echo "\t<td>{$td}</td>\n"; 
} 
echo "</tr>\n"; 
} 

哪个生产:

<tr> 
    <td>1</td> 
    <td>2</td> 
</tr> 
<tr> 
    <td>3</td> 
    <td>4</td> 
</tr> 
<tr> 
    <td>5</td> 
    <td>6</td> 
</tr> 
<tr> 
    <td>7</td> 
    <td>8</td> 
</tr> 
<tr> 
    <td>9</td> 
    <td>10</td> 
</tr> 
<tr> 
    <td>11</td> 
    <td>12</td> 
</tr> 
0

尝试此代码

<?php 
$i=0; 
echo"<table>"; 
echo"<tr>"; 
foreach($aSomeArrayas as $val) 
{ 
if($i %4 ==0) 
    { 
    echo"</tr><tr> <td> $val</td>"; 
    } 
    else 
    { 
    echo"<td> $val </td>"; 
    } 
    $i++; 
} 
echo"</tr>"; 
echo"</table>"; 
?>