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

报告相同问题?

悬赏问题

  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器