我需要使用数组创建和填充表格...现在,博客的一些帮助是这样的:
<?php
$details = array(
1 => array(
1 => 1000,
2 => 'Company A',
3 => '2014-05-10',
4 => '10:00:00',
5 => '15:00:00',
),
2 => array(
1 => 2000,
2 => 'Company A',
3 => '2014-05-11',
4 => '10:00:00',
5 => '15:00:00',
),
3 => array(
1 => 3000,
2 => 'Company B',
3 => '2014-05-10',
4 => '10:00:00',
5 => '15:00:00',
),
4 => array(
1 => 4000,
2 => 'Company B',
3 => '2014-05-11',
4 => '16:00:00',
5 => '19:00:00',
)
);
// Format our data into something we can use more easily
$flight_dates = array();
$times = array();
$dates = array();
foreach ($details as $flight_details) {
$company_name = $flight_details[2];
$date = $flight_details[3];
$time = $flight_details[4] . ' - ' . $flight_details[5];
// Keep a unique record of each date, and the times of flights
$dates[$date] = 1;
$times[$time] = 1;
// Record which date/time each company is flying
$flight_dates[$date][$time][] = $company_name;
}
// Create the table header
$html = '<table border="1">';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th> </th>';
foreach ($dates as $date => $value1) {
$html .= '<th>' . $date . '</th>';
}
$html .= '</tr>';
// Create the rows in the table
foreach ($times as $time => $value1) { // Loop through each flight time
$html .= '<tr>';
$html .= '<td>' . $time . '</td>'; // The first column is always the time
foreach ($dates as $date => $value2) { // Loop through each date
if (!empty($flight_dates[$date][$time])) { // Check if flights exist at the current time
$html .= '<td>' . implode(', ', $flight_dates[$date][$time]) . '</td>'; // List companies
} else { // No flights
$html .= '<td> </td>'; // Leave cell blank
}
}
$html .= '</tr>';
}
$html .= '</table>';
echo $html;
?>
结果如下:
|....................|.. 2014-05-10 ........| 2014-05-11|
|10:00:00 - 15:00:00 | Company A, Company B | Company A |
|16:00:00 - 19:00:00 |......................| Company B |
我希望得到这个:
|....................|2014-05-10 | 2014-05-11|
|10:00:00 - 15:00:00 | Company A | Company A |
|10:00:00 - 15:00:00 | Company B |...........|
|16:00:00 - 19:00:00 |...........| Company B |
在我的想法中,交易是:$times
应该拥有所有$flight_details
数组的所有时间,然后我们填充,将为每个$times
记录创建一行,这样我们不需要内爆,只需将直接值从阵列中删除即可。问题是实现这个......
答案 0 :(得分:1)
如果你想要这样的格式,首先你需要将标题2014-05-10 ... etc
分开,因为它们在上面。然后你需要处理格式。关键是:每个时段都将其分配给公司。考虑这个例子:
<?php
$details = array( 1 => array( 1 => 1000, 2 => 'Company A', 3 => '2014-05-10', 4 => '10:00:00', 5 => '15:00:00', ), 2 => array( 1 => 2000, 2 => 'Company A', 3 => '2014-05-11', 4 => '10:00:00', 5 => '15:00:00', ), 3 => array( 1 => 3000, 2 => 'Company B', 3 => '2014-05-10', 4 => '10:00:00', 5 => '15:00:00', ), 4 => array( 1 => 4000, 2 => 'Company B', 3 => '2014-05-11', 4 => '16:00:00', 5 => '19:00:00', ), 5 => array( 1 => 5000, 2 => 'Company C', 3 => '2014-05-10', 4 => '10:00:00', 5 => '15:00:00', ), 6 => array( 1 => 5000, 2 => 'Company D', 3 => '2014-05-12', 4 => '10:00:00', 5 => '15:00:00', ),);
//properly sort the raw data
// get headers
$header_dates = array();
foreach($details as $value) {
$header_dates[] = $value[3];
}
$header_dates = array_unique($header_dates);
// reformat main data companies
foreach($details as $key => $value) {
$time = "$value[4] - $value[5]"; // use this as key
foreach($header_dates as $date) {
// simple initialization
if(!isset($final[$value[2]][$time][$date])) $final[$value[2]][$time][$date] = '';
}
$final[$value[2]][$time][$value[3]] = $value[2];
// then set them accordingly to their date and time
}
?>
<!-- format them inside the table -->
<table border="1" cellpadding="10">
<!-- print the headers -->
<thead><tr><td></td><?php foreach($header_dates as $value): ?><th><?php echo $value; ?></th><?php endforeach; ?></tr></thead>
<tbody>
<?php foreach($final as $key => $data): ?>
<?php foreach($data as $index => $element): ?>
<tr><td><?php echo $index; ?></td> <!-- loop they time -->
<?php foreach($element as $v):?>
<td><?php echo $v; ?></td> <!-- values (Company A, ...) -->
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>
答案 1 :(得分:0)
为了使结果像预期的一样,你需要给出:
<td>{explode("," $company_name)[0]}</td>
<td>{explode("," $company_name)[1]}</td>
在标题中,您需要这样:
<td colspan="2">{$date}</td>
注意: 我提供的代码只是一个伪代码。你需要编写自己的代码。我们可以为您提供指导!