如何在php中从数组创建表

时间:2015-05-07 12:04:59

标签: php arrays

我在PHP数组中获取类似于

的数据
([data] => Array ( [1048] => Array ( [email] => email1@gmail.com [name] => myname  [comment] => hello [address] => myaddress [city] => my city [country] => mycountry [state] =>my state [phone] => 99999999 [status] => 1 [zipcode] =>22223 [enable] => 1 [enable_read_only] => 0 [send_reports] => 1 [enable_change_password] => 1 [leverage] => 200 [regdate] => 1422613512 ) 

[1115] => Array ( [email] => email2@gmail.com [name] => myname [comment] => hello [address] => XYZ, 123 [city] => Ney York [country] => United States [state] => XYZ [phone] => 1988877777 [status] => [zipcode] => 122002 [enable] => 1 [enable_read_only] => 0 [send_reports] => 1 [enable_change_password] => 1 [leverage] => 100 [regdate] => 1424980102 )))

我需要以表格格式显示它。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

使用foreach,应该很简单:

<?php

$array = array(
    'data' => array (
        1048 => array(
            'email' => 'XXX',
            'name' => '...',
            'comment' => '...'
        ),
        1115 => array(
            'email' => 'XXX',
            'name' => '...',
            'comment' => '...'
        )
    )
);

echo '<table>';
echo '<tr>';
    echo '<th>ID';
    echo '<th>Email';
    echo '<th>Name';
    echo '<th>Comment';

    foreach ($array['data'] as $id => $row) {
        echo '<tr>';
            echo '<td>' . $id;
            echo '<td>' . $row['email'];
            echo '<td>' . $row['name'];
            echo '<td>' . $row['comment'];
    }
echo '</table>';

?>

答案 1 :(得分:-1)

找到循环方式

    foreach($data as $k=>$v)
{

//keys as your header
//values in td
}