我有一些从DB获得的PHP数组,需要进行组合,以便将其呈现为HTML表视图。但我不确定完成这项工作的最佳方式。这是我的数组结构。
ArrayA = array(
0 => array(
"ID" => 1,
"FullName" => "John Doe",
"BloodType" => "B"
),
1 => array(
"ID" => 2,
"FullName" => "Patrick Star",
"BloodType" => "AB"
)
);
ArrayB = array(
0 => array(
"ID" => 1,
"UserID" => 1,
"CHECKUP" => "1ST_MONTH",
"Remarks" => "ABCDE"
),
1 => array(
"ID" => 2,
"UserID" => 1,
"CHECKUP" => "3RD_MONTH",
"Remarks" => "FGHIJ"
),
2 => array(
"ID" => 3,
"UserID" => 2,
"CHECKUP" => "1ST_MONTH",
"Remarks" => "KLMNO"
),
4 => array(
"ID" => 4,
"UserID" => 1,
"CHECKUP" => "1ST_MONTH",
"Remarks" => "PQRST"
)
);
ArrayC = array(
0 => array(
"ID" => 1,
"UserID" => 1,
"DonorDate" => "2020-12-01",
"Height" => 180,
"Weight" => 70,
"RiskScore" => 4
),
1 => array(
"ID" => 1,
"UserID" => 2,
"DonorDate" => "2020-12-01",
"Height" => 160,
"Weight" => 50,
"RiskScore" => 10
)
);
ArrayD = array(
0 => array(
"ID" => 1,
"UserID" => 1,
"Visit" => "Visit 1",
"Remarks" => "ABC"
),
1 => array(
"ID" => 2,
"UserID" => 1,
"Visit" => "Visit 2",
"Remarks" => "CDE"
),
2 => array(
"ID" => 3,
"UserID" => 2,
"CHECKUP" => "Visit 1",
"Remarks" => "FGH"
)
);
我想把它呈现成这样一个HTML表。
<table border="1">
<thead>
<tr>
<td>ID</td>
<td>Blood Type</td>
<td>Full Name</td>
<td>1st Month</td>
<td>2nd Month</td>
<td>3rd Month</td>
<td>Donor Date</td>
<td>Height</td>
<td>Weight</td>
<td>Risk Score</td>
<td>Post Visit 1</td>
<td>Post Visit 2</td>
<td>Post VIsit 3</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>B</td>
<td>John Doe</td>
<td>ABCDE</td>
<td></td>
<td>FGHIJ</td>
<td>2020-12-01</td>
<td>180</td>
<td>70</td>
<td>4</td>
<td>ABC</td>
<td>CDE</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>PQRST</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>AB</td>
<td>Patrick Star</td>
<td>KLMNO</td>
<td></td>
<td></td>
<td>2020-12-01</td>
<td>160</td>
<td>50</td>
<td>10</td>
<td>FGH</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
那么,这有可能吗?或者有其他解决方案吗?请注意,数组的数据集很大。
您可以在另一个for循环中使用foreach循环来获取元素。
for ($i=0 ; $i < count($ArrayA); $i++) {
foreach ( $ArrayA[$i] as $k => $m) {
if($k == 'ID')
echo "<td>$m</td>";
else
echo "<td></td>";
}
}