我从mysql数据库获得以下数组,在获取所有数据后,我应创建一个表来显示所有值。
Array
(
[0] => Array
(
[groupNo] => 1001
[name] => james
[id] => 1
)
[1] => Array
(
[groupNo] => 1002
[name] => chen
[id] => 2
)
[2] => Array
(
[groupNo] => 1002
[name] => ash
[id] => 3
)
[3] => Array
(
[groupNo] => 1001
[name] => mark
[id] => 4
)
)
我目前的表格就像这样:
Group Number | Name | ID |
-------------+------+---------------------
1001 | James | |
+---------------+------------
| Mark | |
-------------+----------------------------
1002 | chen | |
+----------------------------
| ash | |
-------------+----------------------------
但我想要的是我的桌子看起来如下:
Group Number | Name | ID |
-------------+------+---------------------
1001 | James | 1 |
+---------------+------------
| Mark | 4 |
-------------+----------------------------
1002 | chen | 2 |
+----------------------------
| ash | 3 |
-------------+----------------------------
在此之前,我已经尝试了所有内容并在代码中进行了大量更改,但仍无法在右列中插入ID号。
以下是我的代码:
$newArray = array();
foreach($sharingGroup as $item) {
$newArray[$item['groupNo']][] = $item['name'];
}
if (count($newArray) > 0) {
//$sharingcontactTable = '';
foreach($newArray as $key => $val) {
$sharingcontactTable .= "<tr>\r\n";
$sharingcontactTable .= "<td rowspan='".count($val)."'>{$key}</td>\r\n";
foreach($val as $key => $td) {
if($key>0) {
$sharingcontactTable.= "<tr>";
}
$sharingcontactTable .= "<td>{$td}</td>\r\n";
$sharingcontactTable .= "<td>!! No of ID should in here !!</td>\r\n";
$sharingcontactTable .= "</tr>\r\n";
}
}
}
任何人都可以帮我解决这个问题。
答案 0 :(得分:1)
简单修复:
foreach($sharingGroup as $item) {
$newArray[$item['groupNo']][] = array($item['name'], $item['id']);
}
然后在循环中:
$sharingcontactTable .= "<td>{$td[0]}</td>\r\n";
$sharingcontactTable .= "<td>{$td[1]}</td>\r\n";