简体   繁体   English

遍历数组 <td> 细胞

[英]Looping through Array in multiple <td> cells

Lets say I have an array 可以说我有一个数组

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

Now displaying one array value in one table row is easy like this 现在像这样在一个表行中显示一个数组值很容易

echo "<table>";

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

echo "</table>";

But I want to display the values in table format like this 但是我想像这样以表格格式显示值

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

How can I achieve this ? 我该如何实现?

Edited, this works: 编辑后,此方法有效:

$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";

Just a thought which seems nicer IMHO. 只是一个想法似乎更好恕我直言。

<?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;
}

will create a matrix array of variable $width You can then use that data as a double for-each like this: 将创建一个变量$width的矩阵数组,然后可以将该数据用作double 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";
}

Which produces: 产生:

<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>

Just do a running count. 只是做一个连续的计数。 So before the foreach set $i = 1 and every fourth result, reset the count to $i = 1 and then end the row and re-open a new row. 因此,在foreach设置$i = 1并获得每四个结果前,将计数重置为$i = 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>";
echo "<table>";
echo "<tr>";
foreach ($aSomeArray as $aSomeArrayKey => $aSomeArrayValue)
{   
 echo "<td>".$aSomeArrayValue."</td>";
 if($aSomeArrayValue % 4 == 0)
     echo "</tr><tr>";
}
echo "</tr>";
echo "</table>";
$i=1;
echo "<tr>";
foreach ($aSomeArray as $aSomeArrayKey => $aSomeArrayValue)
{   

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

It will also work for the indivisible by 4 number of elements. 它也适用于不可分割的4个元素。

Try this code 试试这个代码

<?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>";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM