所以当我有像这样的嵌套for循环时
<?php for ($i= 0; $i< 5; $i++)
{
for ($x= 0; $x< 5; $x++)
{
echo $x;
}
echo "<br/>";
} ?>
它返回:
01234
01234
01234
01234
01234
那么为什么下面的嵌套for循环不起作用?它不是再次启动每个$x
,而是将它们组合在一起。所以(假设结果与上面的例子相同)第一行将是01234
,但第二行将是0123456789
,01234
与第一行相同。, 等等。因此第8行将包含第1行到第8行的所有值。
<?php
try {
$sql = "SELECT timetable.id as id, timetable.day as serviceday, timetable.station, timetable.departs as time, CURTIME(),
CASE WHEN ROUND(TIME_TO_SEC(timetable.departs)/60 - TIME_TO_SEC(CURTIME())/60,0) > 120 THEN FLOOR(ROUND(TIME_TO_SEC(timetable.departs)/60 - TIME_TO_SEC(CURTIME())/60,0)/60) ELSE ROUND(TIME_TO_SEC(timetable.departs)/60 - TIME_TO_SEC(CURTIME())/60,0) END as departs,
CASE WHEN ROUND(TIME_TO_SEC(timetable.departs)/60 - TIME_TO_SEC(CURTIME())/60,0) <= 120 THEN 'min' ELSE 'hrs' END as units, DATE_FORMAT(timetable.departs, '%l:%i %p') as time2,
CASE WHEN timetable.platform != platformchange.platform THEN platformchange.platform ELSE timetable.platform END as platform,
timetable.route, timetable.run, route.code, line.name, line.translink, station.name as terminus, line.colour, ns, a.departs as terminustime, IFNULL(c.id,-1) as cancel FROM timetable INNER JOIN route ON timetable.route = route.id INNER JOIN line ON route.line = line.id INNER JOIN station ON route.terminus = station.id INNER JOIN (SELECT departs, station, route, run FROM timetable) a ON a.station = station.id AND timetable.route = a.route AND timetable.run = a.run LEFT JOIN (SELECT service, platform FROM platformchange WHERE day LIKE '%" . $day . "%') as platformchange ON timetable.id = platformchange.service LEFT JOIN overnight ON timetable.route = overnight.route AND timetable.run = overnight.run LEFT JOIN (SELECT cancel.id, cancel.route, cancel.run FROM cancel WHERE cancelday = CURDATE()) as c ON c.route = timetable.route AND c.run = timetable.run WHERE timetable.station = " . $_GET['id'] . " AND timetable.departs > CURTIME() AND (timetable.day LIKE '%" . $day . "%' OR timetable.date = CURDATE()) AND IFNULL(c.id,-1) < 1 GROUP BY timetable.id ORDER BY time ASC LIMIT 12";
$s = $pdo->prepare($sql);
$s->execute();
}
catch (PDOException $e){
}
foreach($s as $row){
$services[] = array('id' => $row['id'], 'time' => $row['time'], 'platform' => $row['platform'], 'terminus' => $row['terminus'], 'colour' => $row['colour'], 'departs' => $row['departs'] . ' ' . $row['units'], 'route' => $row['route'], 'run' => $row['run'], 'time2' => $row['time2'], 'ns' => $row['ns'], 'terminustime' => $row['terminustime'], 'tl' => $row['translink']);
}
for($i = 0; $i < count($services); $i++){
try {
$query = "SELECT station.name as station, station.distancefromcentral, ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0) as orderby, CASE WHEN ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0) > 120 THEN FLOOR(ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0)/60) ELSE ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0) END as departs, CASE WHEN ROUND(TIME_TO_SEC(departs)/60 - TIME_TO_SEC(CURTIME())/60,0) > 120 THEN 'hrs' ELSE 'min' END as units, timetable.route, timetable.run, day, date, station.distancefromcentral
FROM timetable
INNER JOIN station ON timetable.station = station.id
LEFT JOIN overnight ON overnight.route = timetable.route AND overnight.run = timetable.run
WHERE (departs > '" . $services[$i]['time'] . "' AND (day LIKE '%" . $day . "%' OR date LIKE CURDATE()) AND timetable.route = " . $services[$i]['route'] . "
AND timetable.run = " . $services[$i]['run'] . ")
OR ( overnight.id > 0 AND(day LIKE '%" . $nextday . "%' OR date LIKE DATE_ADD(CURDATE(), INTERVAL 1 DAY) ) AND timetable.route = " . $services[$i]['route'] . "
AND timetable.run = " . $services[$i]['run'] . ")
ORDER BY date,
CASE
WHEN DATE_FORMAT(CURDATE(),'%w') = 0
THEN `day`
END DESC,
CASE
WHEN DATE_FORMAT(CURDATE(),'%w') <> 0
THEN `day`
END ASC,
orderby ASC";
$z = $pdo->prepare($query);
$z->execute();
}
catch (PDOException $error){
echo 'error';
}
foreach($z as $z){
$stops[] = array('station' => $z['station'], 'departs' => $z['departs'] . ' ' . $z['units'], 'dist' => $z['distancefromcentral'], 'route' => $z['route'], 'run' => $z['run'], 'day' => $z['day'], 'orderby' => $z['orderby'], 'units' => $z['units']);
}
if(isset($stops)){
for($x = 0; $x < count($stops); $x++){
echo $x;
}
}
echo '<br/>';
}
?>
所以这是一个只有2行的例子
012345678910111213
012345678910111213141516
但是第二行只有3个值,所以应该只有012
。它不是按$i
分隔它们,只是将它们组合起来
如果我没有好好解释,我道歉
答案 0 :(得分:2)
我相信你的问题就是这部分
foreach($z as $z){
$stops[] = array('station' => $z['station'], 'departs' => $z['departs'] . ' ' . $z['units'], 'dist' => $z['distancefromcentral'], 'route' => $z['route'], 'run' => $z['run'], 'day' => $z['day'], 'orderby' => $z['orderby'], 'units' => $z['units']);
}
在你添加到$stops
的每个循环上,所以如果第一个有14个,那么第二个会增加3个,所以17个不仅仅是3个。
您可以在
之前重置阵列$stops = array();
foreach($z as $z){
...
或者将$i
添加为关键
foreach($z as $z){
$stops[$i][] = array('station' => $z['station'], 'departs' => $z['departs'] . ' ' . $z['units'], 'dist' => $z['distancefromcentral'], 'route' => $z['route'], 'run' => $z['run'], 'day' => $z['day'], 'orderby' => $z['orderby'], 'units' => $z['units']);
}
if(isset($stops[$i])){
for($x = 0; $x < count($stops[$i]); $x++){
echo $x;
}
}