我有一个多维数组,如下所示。我想在表格中显示数据,将一周中的每一天及其开始和结束时间显示在一行中。下表附有预期结果。
我对数组不熟悉,所以我还没有真正尝试过任何值得一提的东西。非常感谢您的帮助。非常感谢。
Array
(
[opening_time] => Array
(
[monday] => Array
(
[0] => 10:00 am
[1] => 6:30 pm
)
[tuesday] => Array
(
[0] => 12:30 pm
)
[wednesday] => Array
(
[0] => 4:30 pm
)
[thursday] => Array
(
[0] => 2:30 pm
)
[friday] => Array
(
[0] => 4:00 pm
)
[saturday] => Array
(
[0] => 6:00 am
)
[sunday] => Array
(
)
)
[closing_time] => Array
(
[monday] => Array
(
[0] => 6:00 pm
[1] => 10:30 pm
)
[tuesday] => Array
(
[0] => 7:00 pm
)
[wednesday] => Array
(
[0] => 10:00 pm
)
[thursday] => Array
(
[0] => 6:30 pm
)
[friday] => Array
(
[0] => 11:00 pm
)
[saturday] => Array
(
[0] => 6:00 pm
)
[sunday] => Array
(
)
)
)
你可以这样做。请阅读代码中的注释以及输出下面的注释。
<?php
$times = [
"opening_time" =>[
"monday" => [
0 => "10:00 am",
1 => "6:30 pm"
],
"tuesday" => [
0 => "12:30 pm"
],
"wednesday" => [
0 => "4:30 pm"
],
"thursday" => [
0 => "2:30 pm"
],
"friday" => [
0 => "4:00 pm"
],
"saturday" => [
0 => "6:00 am"
],
"sunday" => []
],
"closing_time" => [
"monday" => [
0 => "6:00 pm",
1 => "10:30 pm"
],
"tuesday" => [
0 => "7:00 pm"
],
"wednesday" => [
0 => "10:00 pm"
],
"thursday" => [
0 => "6:30 pm"
],
"friday" => [
0 => "11:00 pm"
],
"saturday" => [
0 => "6:00 pm"
],
"sunday" => []
]
];
// Repacking your original array to be like this
/*
$arr = [
day => [
opening_time => [
10:00 am
6:30 pm
],
closing_time => [
6:00 pm
10:30 pm
]
]
...
];
*/
$repacked = [];
foreach($times as $type=>$days) {
foreach($days as $day=>$time) {
if(!isset($repacked[$day][$type])) {
$repacked[$day][$type] = [];
}
$repacked[$day][$type][] = $time;
}
}
$html = '
<table>
<thead>
<tr>
<th>Day</th>
<th>Working hours 1</th>
<th>Working hours 2</th>
</tr>
</thead>
<tbody>
';
// Loop through days...
foreach($repacked as $day=>$times) {
$html .= '
<tr>
<td>'.ucfirst($day).'</td>
';
// ... then through specific opening times
foreach($times as $type=>$time) {
// We've already picked this up during one of
// the previous loop passes, we don't need it now
if($type === "closing_time") {
continue;
}
// Skip days with no set time
if(!$time[0]) {
$html .= '
<td></td>
<td></td>
';
continue;
}
// With the outline of the repacked array given in
// lines 51-63, $time[0][0] is, in the first run through
// the loop:
/*
$repacked
$day (e.g. Monday)
$type (e.g. opening_time)
$time (a nested array, e.g. [ [10:00 am, 6:30 pm] ])
$time[0] ( first element of the nested array, e.g. [10:00 am, 6:30 pm] )
$time[0][1] (first time, e.g. 10:00 am)
*/
// Check the first part of the day
if(isset($time[0][0])) {
$html .= '
<td>'.$time[0][0].' to '.$repacked[$day]["closing_time"][0][0].'</td>
';
} else {
$html .= '
<td></td>
';
}
// Check the second part of day
if(isset($time[0][1])) {
$html .= '
<td>'.$time[0][1].' to '.$repacked[$day]["closing_time"][0][1].'</td>
';
} else {
$html .= '
<td></td>
';
}
}
$html .= '
</tr>
';
}
$html .= '
</tbody>
</table>
';
echo $html;
?>
上面的代码输出(逐字、空行和全部):
<table>
<thead>
<tr>
<th>Day</th>
<th>Working hours 1</th>
<th>Working hours 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<td>10:00 am to 6:00 pm</td>
<td>6:30 pm to 10:30 pm</td>
</tr>
<tr>
<td>Tuesday</td>
<td>12:30 pm to 7:00 pm</td>
<td></td>
</tr>
<tr>
<td>Wednesday</td>
<td>4:30 pm to 10:00 pm</td>
<td></td>
</tr>
<tr>
<td>Thursday</td>
<td>2:30 pm to 6:30 pm</td>
<td></td>
</tr>
<tr>
<td>Friday</td>
<td>4:00 pm to 11:00 pm</td>
<td></td>
</tr>
<tr>
<td>Saturday</td>
<td>6:00 am to 6:00 pm</td>
<td></td>
</tr>
<tr>
<td>Sunday</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
测试一下这里。
注释
我重新打包了初始数组,以便更轻松地循环它并生成稍后渲染的 HTML 字符串。如果您控制初始表(我在提供的代码中称为
$times
的表)的形成方式,则可以避免重新打包。
渲染的 HTML 表是一个准系统表 - 您可以在循环遍历它时提供 CSS 类,或者在包含
$html
和 thead
的 tbody
变量的开头部分提供。
此外,如果您需要设置非空(或空)表格单元格的样式,也可以使用 CSS 类来实现。
一些注意事项
所提供的建议假设工作时间始终分为两部分。考虑到这一假设,该用法的范围受到一定限制 - 它不涵盖只有单一工作时间的情况(例如,每天上午 9 点到下午 5 点,仅此而已),也不涵盖有工作时间的情况。是三、四或 n 部分的工作时间。
考虑到这一点,代码可以通过几种方式进行概括(我不会这样做,因为你实际上并不需要它,但会提供一个总体轮廓):
th
的数量动态化,而不是使用硬编码的表头,只需找到最大的 opening_time
数组,然后使用简单的 th
循环创建这些额外的 for
td
时应用,因为它们不会是 3 个,而是 1 + n,其中 n 是最大 opening_time
数组的大小if/else
检查