我试图制作一个整洁的,结构化的列表,在这种情况下,电影和showdates。
$shows = array(
array(
"Thursday" => array(
"17:00",
"19:00")),
array(
"Friday" => array(
"16:30",
"18:45"
"20:10")),
array(
"Saturday" => array(
"18:30",
"21:00"))
);
问题是我似乎无法以合理的方式打印出来。 在这种情况下,这些日子应该是动态的,而不是硬条件。
for ($row = 0; $row < $shows.length(); $row++) //Haven't got a clue about the 'length()'
{
print $shows[$row] . "<br>"; //Print the day.
for (
$col = 0; $col < $shows[$row].length(); $col++) //Loop through each day.
{
print (">" . $shows[$row][$col] . "<br>"); //Print each time of the day.
}
}
我想要做的就是每天用相应的时间打印出来。 应该是这样的。
Thursday - 17:00
19:00
Friday - 16:30
18:45
20:10
答案 0 :(得分:1)
foreach ($shows as $show) {
foreach ($show as $day => $times) {
echo $day;
foreach ($times as $time) {
echo $time;
}
}
}
但是,实际上,你应该像这样简化一下:
$shows = array(
array('day' => 'Saturday', 'times' => array('17:00', '19:00')),
…
);
foreach ($shows as $show) {
echo $show['day'];
foreach ($show['times'] as $time) {
echo $time;
}
}
或者,以非常正确的方式并以计算机可解析的方式:
$shows = array(
strtotime('2010-12-24 17:00:00'),
strtotime('2010-12-24 19:00:00'),
…
);
$lastDate = null;
foreach ($shows as $show) {
if (!$lastDate || date('Y-m-d', $show) != date('Y-m-d', $lastDate)) {
echo date('l', $show);
}
echo date('H:i', $show);
$lastDate = $show;
}
:○)
答案 1 :(得分:0)
在PHP中获取数组中元素的数量,使用count函数。
for ($row = 0; $row < count($shows); $row++) {
foreach($shows[$row] as $key => $vals) {
echo "$key - ";
$first = true;
foreach($vals as $val) {
if($first) {
echo "\t$val\n";
$first = false;
} else {
echo "\t\t$val\n";
}
}
}
}