PHP foreach 循环不递增

标签 php

如果这是一个微不足道的问题,我深表歉意。我进行了搜索,但没有找到我的问题的答案,所以我发布了。预先感谢所有可以帮助我的人,并感谢您的宝贵时间。我正在尝试对 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 循环应该自动递增。

最佳答案

只需将循环中的 $reports -> report 替换为 $value 即可使其正常工作。

似乎 SimpleXMLElement 允许使用 $reports->report 作为可枚举对象和简单对象,以便在没有多个相同标签的情况下进行简化。

这很可能就是您没有收到错误的原因!

关于PHP foreach 循环不递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11804818/

相关文章:

Java 和 PHP POST 请求不起作用

PHP:使用动态端口分配连接到 MS SQL Server

php - 在数据库中存储 Paypal 详细信息

php - 德鲁帕尔 "You are not authorized to access this page."

php - 通过 PHP 执行。我无法将输出(stderr)放入文件中

php - 在 Kohana 中请求::$controller

php - PHP 中 Python 的 Lambda 等效项

javascript - 表的每一行上的提交按钮,将该行的数据插入数据库

php - 在 PHP 中替换数组键中的子字符串

php - 如何在每篇文章下方添加 'read more' 按钮?