2012-08-04 156 views
2

我很抱歉,如果这是微不足道的问题。我做了搜索,没有找到我的问题的答案,所以我张贴。非常感谢谁能帮助我,并感谢您的时间。我试图对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循环应该自动递增。

+2

你在哪里使用你正在循环的键/值对? – Sebas 2012-08-04 00:15:42

回答

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 />'; 
      } 
0

你正在让你的foreach语法错误。试着用$value

3

更换$reports -> report在foreach块内只需更换$reports -> report在循环通过$value得到它的工作。

看起来SimpleXMLElement允许使用$reports->report作为枚举和简单的对象来简化没有多个相同标记的情况。

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

+0

感谢您的补充信息。我想知道为什么海报使用的代码甚至给出了工作的外观。 – octern 2012-08-04 00:21:36

3

您没有使用foreach循环中递增的数据。你想要做这样的事情,而不是:

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

在代码中,你通过$reports->report迭代,为$key => $value,但使用的迭代值($key$value),你叫$reports->report,不会改变作为循环迭代。

0

您不是指循环中的$ key和$ value都没有!你总是指$ reports-> report ['case_number'],它总是相同的。

编辑:我忘了提及你应该重申$报告。