我很抱歉,如果這是微不足道的問題。我做了搜索,沒有找到我的問題的答案,所以我張貼。非常感謝誰能幫助我,並感謝您的時間。我試圖對api(sanfrancisco.crimespotting.org)返回的犯罪數據做一個簡單的分析,並且遇到一個障礙。下面非常簡單的代碼。PHP foreach循環不遞增
<?
$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個報告,當我測試我的瀏覽器中的網址時,我看到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循環應該自動遞增。
你在哪裏使用你正在循環的鍵/值對? – Sebas 2012-08-04 00:15:42