使用php显示输入格式

时间:2011-06-29 07:39:43

标签: php for-loop while-loop

我需要在三列中显示10个数字的数组。如果我添加另一个数字,11,它必须添加到10以下。因为数字增加行可以增加而不是列,任何人都可以说?

1 4 7 10
2 5 8
3 6 9
我在第四栏中获得了10分,但我在第三栏中需要它。并且行会像

那样增加
1 4 8
2 5 9
3 6 10
4 7

2 个答案:

答案 0 :(得分:1)

在表格中显示输出..

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

$row= ceil(count($arr)/3);
echo "<table border='1'>";
for($i = 1; $i <= $row; $i++) {
    echo "<tr>";
    echo "<td>". $i ."</td>";
    $k = 0;
    $pre = 0;
  for($j = 1; $j <= 2; $j++) {
    if($pre == 0)
      $pre = $k = $i + $row;
    else
      $pre = $pre + $row;

    if($pre <= max($arr))
      echo "<td>". $arr[$pre-1] ."</td>";
  }
  echo "</tr>";
}
echo "</table>";
?>

输出将是:

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

答案 1 :(得分:0)

试试这个......

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

$row= ceil(count($arr)/3);

for($i = 1; $i <= $row; $i++) {
    echo $i;
    $k = 0;
    $pre = 0;
  for($j = 1; $j <= 2; $j++) {
    if($pre == 0)
      $pre = $k = $i + $row;
    else
      $pre = $pre + $row;

    if($pre <= max($arr))
      echo " ". $arr[$pre-1] ." ";
  }
  echo "<br>";
}

?>

14元素时的输出:

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

11元素时的输出:

1 5 9
2 6 10
3 7 11
4 8 

13个元素时的输出:

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