array( -6ren">
gpt4 book ai didi

php - PHP 和 MySQL 中嵌套数组的输出/结构出现问题

转载 作者:行者123 更新时间:2023-11-29 14:56:34 25 4
gpt4 key购买 nike

我正在尝试制作一个整洁、结构化的列表,在本例中是电影和放映日期。

$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

最佳答案

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;
}

:o)

关于php - PHP 和 MySQL 中嵌套数组的输出/结构出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4489434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com