PHP foreach循环不递增

时间:2012-08-04 00:11:35

标签: 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循环应该自动递增。

5 个答案:

答案 0 :(得分:3)

只需将$reports -> report中的$value替换为SimpleXMLElement即可。

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

这很可能就是为什么你没有得到错误!

答案 1 :(得分:3)

您没有在foreach循环中使用递增的数据。你想做这样的事情:

foreach ($reports->report as $report)
{
    echo '<br />';
    echo '<b>Case #:</b> ' . $report['case_number'];
    // ...
}

在您的代码中,您正在$reports->report进行迭代,而不是使用迭代值($key => $value$key),而是调用{{1}随着循环的迭代,它不会改变。

答案 2 :(得分:0)

你的foreach语法错误了。尝试使用$reports -> report

替换foreach块内的$value

答案 3 :(得分:0)

你在循环中既不指$ key也不指$ value!您总是指$ reports-&gt; report ['case_number'],它始终是相同的。

编辑:我忘记提及你了解报告。

答案 4 :(得分:0)

我要感谢所有那些快速帮助的人。在更多地回顾了PHP文档(特别是http://tinyurl.com/36a4aet)之后,我发现我需要在$ reports(作为其根标记)上使用children()方法来获取每个报告,然后为每个报告标记调用attributes()方法获取报告的其余数据。下面的新代码似乎已经成功了:)。

            foreach ($reports->children() as $node)
            {
                    $attribs = $node->attributes();
                    echo '<br />';
                    echo '<b>Case #:</b> '.$attribs["case_number"];
                    echo '<br />';
                    echo '<b>Crime type:</b> '.$attribs["crime_type"];
                    echo '<br />';
                    echo '<b>Date/Time:</b> '.$attribs["date_time"];
                    echo '<br />';
                    echo '<b>Date </b>: '.$attribs["date"];
                    echo '<br />';
                    echo '<b>Time:</b> '.$attribs["time"];
                    echo '<br />';
                    echo '<b>More Info: </b> <a href='.$attribs["href"].'>'.$attribs["href"].'</a>';
                    echo '<br />';
                    echo '<br />';
            }