如果这是一个微不足道的问题,我深表歉意。我进行了搜索,但没有找到我的问题的答案,所以我发布了。预先感谢任何可以帮助我的人,并感谢您抽出宝贵的时间。我正在尝试对 api (sanfrancisco.crimespotting.org) 返回的犯罪数据进行简单解析并遇到障碍。下面的代码非常简单。
<?
$url = "http://sanfrancisco.crimespotting.org/crime-data?format=xml&count=5&dstart=2009-04-20";
$reports = new SimpleXMLElement($url, NULL, TRUE);
foreach ($reports-> report as $key => $value)
{
echo '<br />';
echo '<b>Case #:</b> '.$reports -> report['case_number'];
echo '<br />';
echo '<b>Crime type</b> '.$reports -> report['crime_type'];
echo '<br />';
echo '<b>Date/Time:</b> '.$reports -> report['date_time'];
echo '<br />';
echo '<b>Date </b> '.$reports -> report['date'];
echo '<br />';
echo '<b>Time:</b> '.$reports -> report['time'];
echo '<br />';
echo '<b>More Info </b> '.$reports -> report['href'];
echo '<br />';
echo '<br />';
echo '<br />';
}
?>
api 返回 5 个报告,因为当我在浏览器中测试 url 时,我看到返回了 5 个报告标签。这是我的浏览器的输出。
<reports>
<report case_number="120220205" crime_type="Vehicle Theft" date_time="2012-07-17T23:10:00-07:00" date="Tuesday, Jul 17, 2012" time="11:10pm" lat="37.772441" lon="-122.412422" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858">STOLEN AUTOMOBILE</report>
<report case_number="120560807" crime_type="Alcohol" date_time="2012-07-16T13:35:00-07:00" date="Monday, Jul 16, 2012" time="1:35pm" lat="37.783288" lon="-122.408954" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-16/Alcohol/334582">
UNDER INFLUENCE OF ALCOHOL IN A PUBLIC PLACE (ARREST, BOOKED)
</report>
<report case_number="120559850" crime_type="Disturbing the Peace" date_time="2012-07-16T13:00:00-07:00" date="Monday, Jul 16, 2012" time="1:00pm" lat="37.778678" lon="-122.416545" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-16/Disturbing_The_Peace/334575">COMMITTING PUBLIC NUISANCE (ARREST, CITED)</report>
<report case_number="120560653" crime_type="Theft" date_time="2012-07-16T12:35:00-07:00" date="Monday, Jul 16, 2012" time="12:35pm" lat="37.784644" lon="-122.414271" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-16/Theft/334580">PETTY THEFT SHOPLIFTING</report>
<report case_number="120560700" crime_type="Theft" date_time="2012-07-16T12:00:00-07:00" date="Monday, Jul 16, 2012" time="12:00pm" lat="37.78966" lon="-122.400934" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-16/Theft/334581">GRAND THEFT BICYCLE</report>
</reports>
如您所见(上图),每个报告标签都有不同的 case_number 属性(例如 120220205、120560807 等)。但是,当我加载页面时,这是我收到的输出。
Case #: 120220205
Crime type Vehicle Theft
Date/Time: 2012-07-17T23:10:00-07:00
Date Tuesday, Jul 17, 2012
Time: 11:10pm
More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858
Case #: 120220205
Crime type Vehicle Theft
Date/Time: 2012-07-17T23:10:00-07:00
Date Tuesday, Jul 17, 2012
Time: 11:10pm
More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858
Case #: 120220205
Crime type Vehicle Theft
Date/Time: 2012-07-17T23:10:00-07:00
Date Tuesday, Jul 17, 2012
Time: 11:10pm
More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858
Case #: 120220205
Crime type Vehicle Theft
Date/Time: 2012-07-17T23:10:00-07:00
Date Tuesday, Jul 17, 2012
Time: 11:10pm
More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858
Case #: 120220205
Crime type Vehicle Theft
Date/Time: 2012-07-17T23:10:00-07:00
Date Tuesday, Jul 17, 2012
Time: 11:10pm
More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858
由于我使用 foreach 循环遍历返回的结果,我不知道为什么第一种情况会重复 5 次,因为我知道 foreach 循环应该自动递增。