它返回: 01234 01234 01234 01234 01234 那么为什么下面的嵌套 for 循环不起作用呢?它不会再次启动每个 $x,而是-6ren">
gpt4 book ai didi

php - 嵌套 for 循环无法正常工作

转载 作者:行者123 更新时间:2023-11-29 08:03:33 25 4
gpt4 key购买 nike

所以当我有这样的嵌套 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 code> 与第一行相同,依此类推。因此第 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 分开,只是将它们组合起来

如果我没有解释清楚,我深表歉意

最佳答案

我相信你的问题是这部分

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

关于php - 嵌套 for 循环无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165354/

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