dsk88199 2012-03-28 04:14
浏览 16
已采纳

使用php数组动态构建和填充表

let's say I have these two arrays:

$array1 = array(1, 2, 3, 4, 5);
$array2 = array(6, 7, 8, 9, 10, 11, 12, 13, 14, 15);

As you can see, my arrays have different lengths. What I'm trying to do is to input these array values into an HTML table with the first column containing the values coming from $array1 and the second column containing the values coming from $array2. So, in this case right here, I should have a table of 10 rows (because $array2 contains 10 elements) and 2 columns (because I have 2 arrays). Also, I cannot know in advance which array is going to have more elements than the other (so, $array1 could be bigger than $array2, they could also have equal sizes). So, depending on which array has more elements, the number of rows in my table should adjust accordingly.

Any idea please?

Thank you

  • 写回答

2条回答 默认 最新

  • dongqin1075 2012-03-28 05:14
    关注
    $array2 = array(6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
    $array1 = array(1, 2, 3, 4, 5);
    $a=count($array1);
    $b=count($array2);
    echo "<table border=1><tr><th>Array1</th><th>Array2</th></tr>";
    if($a > $b)
    {
        for($i=0;$i<$a;$i++)
        {
            echo "<tr><td>".$array1[$i]."</td>";
            echo "<td>".$array2[$i]."</td></tr>";
        }
    }
    if($b > $a)
    {
        for($i=0;$i<$b;$i++)
        {
            echo "<tr><td>".$array1[$i]."</td>";
            echo "<td>".$array2[$i]."</td></tr>";
        }
    }
    echo "</table>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作