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

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?