2 回答
TA贡献1858条经验 获得超8个赞
您可以尝试此以获得所需的结果
$header = ['machine','1.00pm','2.00pm','3.00pm','4.00pm','5.00pm'];
$html = "<table border='1'><tr>";
for($i=0; $i<count($header); $i++){
$html.= "<th>".$header[$i]."</th>";
}
$html .= "</tr>";
$res = [
array_merge(
array_unique(array_column($data1,'machine')),
array_column($data1, 'reading','time')
),
array_merge(
array_unique(array_column($data2,'machine')),
array_column($data2, 'reading','time')
)
];
foreach($res as $key => $value){
$html .= "<tr>";
foreach($header as $ikey => $ivalue){
$index = ($ivalue == 'machine') ? 0 : $ivalue;
$html .= "<td>".$res[$key][$index]."</td>";
}
$html .= "</tr>";
}
$html .= "</table>";
模式二。
$header = ['time','machine1','machine2'];
$html = "<table border='1'><tr>";
for($i=0; $i<count($header); $i++){
$html.= "<th>".$header[$i]."</th>";
}
$res = [
array_merge(
array_column($data1, 'reading','time')
),
array_merge(
array_column($data2, 'reading','time')
)
];
for($i=0;$i<count($res)-1;$i++){
foreach($res[$i] as $key => $value){
$html .= '<tr>';
$html .= '<td>'.$key.'</td>';
$html .= '<td>'.$res[$i][$key].'</td>';
$html .= '<td>'.$res[$i+1][$key].'</td>';
$html .= '</tr>';
}
}
echo $html;
TA贡献1848条经验 获得超6个赞
好吧,这个答案包含了所有表格
<?php
$data1 = array(
'0' => array('time'=> '1.00pm', 'reading' =>'271', 'machine'=>'machine1'),
'1' => array('time'=> '2.00pm', 'reading' =>'261', 'machine'=>'machine1'),
'2' => array('time'=> '3.00pm', 'reading' =>'172', 'machine'=>'machine1'),
'3' => array('time'=> '4.00pm', 'reading' =>'300', 'machine'=>'machine1'),
'4' => array('time'=> '5.00pm', 'reading' =>'142', 'machine'=>'machine1')
);
$data2 = array(
'0' => array('time'=> '1.00pm', 'reading' =>'500', 'machine'=>'machine2'),
'1' => array('time'=> '2.00pm', 'reading' =>'432', 'machine'=>'machine2'),
'2' => array('time'=> '3.00pm', 'reading' =>'109', 'machine'=>'machine2'),
'3' => array('time'=> '4.00pm', 'reading' =>'44', 'machine'=>'machine2'),
'4' => array('time'=> '5.00pm', 'reading' =>'18', 'machine'=>'machine2')
);
echo $data1[0]['machine'];
echo '<table>';
echo '<tr>';
echo "<th>Time</th>";
echo "<th>reading</th>";
echo '</tr>';
foreach ($data1 as $key => $value) {
echo "<tr>";
echo "<td>" . $value['time'] . "</td>";
echo "<td>" . $value['reading'] . "</td>";
echo "</tr>";
}
echo '</table>';
echo '<br>';
echo $data2[0]['machine'];
echo '<table>';
echo '<tr>';
echo "<th>Time</th>";
echo "<th>reading</th>";
echo '</tr>';
foreach ($data2 as $key => $value) {
echo "<tr>";
echo "<td>" . $value['time'] . "</td>";
echo "<td>" . $value['reading'] . "</td>";
echo "</tr>";
}
echo '</table>';
echo '<br>';
echo '<br>';
echo '<table>';
echo "<tr>";
echo "<th>machin</th>";
foreach ($data1 as $key => $value) {
echo "<th>" . $value['time'] . "</th>";
}
echo "</tr>";
echo "<tr>";
echo "<td>machine1</td>";
foreach ($data1 as $key => $value) {
echo "<td>" . $value['reading'] . "</td>";
}
echo "</tr>";
echo "<tr>";
echo "<td>machine2</td>";
foreach ($data1 as $key => $value) {
echo "<td>" . $data2[$key]['reading'] . "</td>";
}
echo "</tr>";
echo '</table>';
echo '<br>';
echo '<br>';
echo '<table>';
echo '<tr>';
echo "<th>Time</th>";
echo "<th>machine1</th>";
echo "<th>machine2</th>";
echo '</tr>';
foreach ($data2 as $key => $value) {
echo "<tr>";
echo "<td>" . $value['time'] . "</td>";
echo "<td>" . $data1[$key]['reading'] . "</td>";
echo "<td>" . $value['reading'] . "</td>";
echo "</tr>";
}
echo '</table>';
echo '<br>';
?>
- 2 回答
- 0 关注
- 143 浏览
添加回答
举报