这是我从api获取数据的php代码,一切正常,但我陷入了循环。当我使用常量值时,即10循环显示,它仅显示10个数据,但我想在常量的位置使用变量,以便它将显示所有数据。
<?php
$number = 10;
for ($i=0; $i <= $number ; $i++) { ?>
<tr>
<td><?php echo $json['TrainRoute'][$i]['SerialNo']; ?></td>
<td><?php echo $json['TrainRoute'][$i]['StationName']; ?></td>
<td><?php echo $json['TrainRoute'][$i]['ScheduleArrival']; ?> / <?php echo $json['TrainRoute'][$i]['ScheduleDeparture']; ?></td>
<td class="train-mobile"><?php echo $json['TrainRoute'][$i]['ActualArrival']; ?> / <?php echo $json['TrainRoute'][$i]['ActualDeparture']; ?></td>
<td class="train-mobile"><?php echo $json['TrainRoute'][$i]['Day']; ?></td>
<td style="color: red;"><?php echo $json['TrainRoute'][$i]['DelayInDeparture']; ?></td>
</tr>
<?php } ?>
答案 0 :(得分:1)
<?php
for ($i=0; $i < sizeof($json['TrainRoute']); $i++) { ?>
<tr>
<td><?php echo $json['TrainRoute'][$i]['SerialNo']; ?></td>
<td><?php echo $json['TrainRoute'][$i]['StationName']; ?></td>
<td><?php echo $json['TrainRoute'][$i]['ScheduleArrival']; ?> / <?php echo $json['TrainRoute'][$i]['ScheduleDeparture']; ?></td>
<td class="train-mobile"><?php echo $json['TrainRoute'][$i]['ActualArrival']; ?> / <?php echo $json['TrainRoute'][$i]['ActualDeparture']; ?></td>
<td class="train-mobile"><?php echo $json['TrainRoute'][$i]['Day']; ?></td>
<td style="color: red;"><?php echo $json['TrainRoute'][$i]['DelayInDeparture']; ?></td>
</tr>
<?php } ?>