php如何将数组以表格形式显示-PHP问题

资源魔 27 0

php将数组以表格方式显示:

<?php
//应用array()语句构造将联络人列表中一切数据申明为一个二维数组,默许下标是程序数字索引
    $contact1 = array(                                             //界说外层数组
    array(1,'高某','A公司','北京市','(010)987654321','gm@Linux.com'),//子数组1
    array(2,'洛某','B公司','上海市','(021)123456789','lm@apache.com'),//子数组2
    array(3,'峰某','C公司','天津市','(022)24680246','fm@mysql.com'),  //子数组3
    array(4,'书某','D公司','重庆市','(023)13579135','sm@php.com')     //子数组4
    );
   //以HTML表格的方式输入二维数组中的每一个元素
    echo '<table border="1" width="600" align="center">';
    echo '<caption><h1>联络人列表</h1></caption>';
    echo '<tr bgcolor="#dddddd">';
    echo '<th>编号</th><th>姓名</th><th>公司</th><th>地点</th><th>德律风</th><th>EMALL</th>';
    echo '</tr>';
    //应用双层for语句嵌套二维数组$contact1,以HTML表格的方式输入
    //应用外层轮回遍历数组$contact1中的行
    for($row=0;$row<count($contact1);$row++)
    {
        echo '<tr>';
        //应用内层轮回遍历数组$contact1 中 子数组的每一个元素,应用count()函数管制轮回次数
        for($col=0;$col<count($contact1[$row]);$col++)
        {
            echo '<td>'.$contact1[$row][$col].'</td>';
        }
        echo '</tr>';
    }
    echo '</table>';

后果以下:

1.jpg

二、联系关系数组(不克不及用for轮回)

$contact2 = array(
    "北京联络人"=>array(1,'高某','A公司','北京市','(010)987654321','gm@linux.com'),
    "上海联络人"=>array(2,'洛某','B公司','上海市','(021)123456789','lm@apache.com'),
    "天津联络人"=>array(3,'峰某','C公司','天津市','(022)246802468','fm@mysql.com'),
    "重庆联络人"=>array(4,'书某','D公司','重庆市','(023)135791357','sm@php.com')
    );
 //创立表格将数组轮回输出
    echo '<table border="1" width="600" align="center">';
    echo '<tr bgcolor="#dddddd">';
    echo '<th>编号</th><th>姓名</th><th>公司</th><th>地域</th><th>德律风</th><th>EMALL</th>';
    echo '</tr>';
    foreach ($contact2 as $key=>$value)
    {
        echo '<tr>';
//foreach外面嵌套一个for轮回也是能够的
        /*for($n=0;$n<count($value);$n++)
        {
            echo "<td>$value[$n]</td>";
        }*/
//foreach外面嵌套foreach

        foreach($value as $mn)
        {
            echo "<td>{$mn}</td>";
        }
        echo '</tr>';
    }
    echo '</table>';

成果以下:

2.jpg保举:php效劳器

以上就是php若何将数组以表格方式显示的具体内容,更多请存眷资源魔其它相干文章!

标签: php 数组 php教程 php故障解决 php使用问题 表格

抱歉,评论功能暂时关闭!