2012-04-18 84 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>"; 
?>