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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题