我已经获得了现在运行良好的代码,但认为将观察到的数据从最新日期开始到最旧日期是有用的。如何只翻转观察到的数据?这是代码(对不起,如果它看起来不正常,仍然可以在这个网站上发布):
<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);
?>
<?php foreach ($xml->RESULTSET[0]->ROW as $MSG) :?>
<?php echo '<h4>', $MSG->MSG_TXT; '</h4>'; ?>
<?php endforeach; ?>
<!-- Table Style -->
<style>
table {border: 2px solid #fff;}
table td {height: 15px;}
table td {border: 1px solid #fff; }
table tr {border: 1px solid #fff; }
table td {padding: 3px; }
</style>
<h2>Observed Data</h2>
<table>
<div style="overflow-x:auto;">
<thead>
<tr>
<td><span style="margin:0px; font-weight:bold">Day</span></td>
<td><span style="margin:0px; font-weight:bold">Time(EST)</span></td>
<td><span style="margin:0px; font-weight:bold">Reservoir Elev.(behind dam)*</span</td>
<td><span style="margin:0px; font-weight:bold">Tailwater Elev.(below dam)*</span></td>
<td><span style="margin:0px; font-weight:bold">Avg Hourly Discharge* </span></td>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET[1]->ROW as $obs) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h2>Predicted Data</h2>
<table>
<div style="overflow-x:auto;">
<thead>
<tr>
<td><span style="margin:25px; font-weight:bold">Day</span></td>
<td><span style="margin:5px; font-weight:bold">Average Inflow* </span</td>
<td><span style="margin:5px; font-weight:bold">Midnight Elevation*</span></td>
<td><span style="margin:5px; font-weight:bold">Average Outflow*</span></td>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET[2]->ROW as $pred) :?>
<tr>
<td><?php echo $pred->PREDICTED_DAY; ?></td>
<td><?php echo $pred->DAILY_AVG_INFLOW; ?></td>
<td><?php echo $pred->MIDNIGHT_ELEV; ?></td>
<td><?php echo $pred->DAILY_AVG_OUTFLOW; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>`
答案 0 :(得分:0)
您可以在输出之前将它们读入数组。
foreach ($xml->RESULTSET[1]->ROW as $obs) {
$resultSet1[] = $obs;
}
foreach ($xml->RESULTSET[2]->ROW as $obs) {
$resultSet2[] = $obs;
}
这将使您更容易扭转它们。一旦他们在阵列中,有各种方法可以做到这一点。您可以使用array_reverse
,带有递减索引的for
循环,或类似的东西:
<?php while ($obs = array_pop($resultSet1)) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endwhile; ?>
答案 1 :(得分:0)
如果无法保证条目顺序(按时间顺序),您可以反向排序:
$observations = array();
foreach ($xml->RESULTSET[1]->ROW as $obs) {
$odate = new DateTime($obs->OBS_DAY . $obs->OBS_HR);
$observations[$odate->getTimestamp()] = $obs;
}
krsort($observations);