dongzhuner6981 2019-05-04 11:45
浏览 132
已采纳

如何将多维PHP数组转换为html表

I'm trying to make a HTML table out of a multidimensional array variable $array2 which is a query(from a mysql database PHP query and Not a self-made query) and is as follows;

Array
(
    [0] => Array
        (
            [0] => M2TYEE
            [1] => Jean
            [2] => Harvey
            [3] => London
            [4] => 0314686334
        )

    [1] => Array
        (
            [0] => E26YBE
            [1] => Tom
            [2] => Cruise
            [3] => New York
            [4] => 0635625735     
        )

)

I want to make the table appear like this



|ID     |FIRST NAME| LAST NAME |  CITY     | PHONE      |
________________________________________________________
|M2TYEE | Jean     |  Harvey   |  London   | 0314686334 |
--------------------------------------------------
|E26YBE | Tom      |  Cruise   |  New York | 0635625735 |

They array may increase to upto say 1000 values so using a while/for loop would be necessary. Any help will be highly appreciated

  • 写回答

2条回答 默认 最新

  • doufocheng6233 2019-05-04 11:54
    关注

    You can use array_merge and foreach for the desired output

    $arr = array_merge([ 0 => ['ID','FIRST NAME','LAST NAME','CITY','PHONE']],$arr);
    $html = '<table border="1">';
    foreach($arr as $row){
      $html .= '<tr>';
      foreach($row as $column){
        $html .= '<td>'.$column.'</td>';
      }
      $html .= '</tr>'; 
    }
    $html .= '</table>';
    echo $html;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Windows 驱动开发版本疑问相关
  • ¥15 MAC 未能打开磁盘映像
  • ¥15 fastcap使用,二维导体输入问题
  • ¥15 hosts修改后不能访问
  • ¥15 关于化学反应速率C++编译问题/FLUENT
  • ¥20 Yolov5训练报错
  • ¥15 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面
  • ¥15 ASP.net Gridview插件数据更新失败问题
  • ¥15 有没有人知道这种提示怎么关?现在不做ts项目了不知道咋关了,求解,现在我以前的js项目都是这种提示了
  • ¥15 为什么mysql做了碎片化处理data_free还是很高