2010-03-23 50 views
0

我有100条记录一个XML文件,但我想它限制它仅仅5个记录限制XML阅读到5个记录

for ($i=0;$i<=5;$i++) { 
    foreach($xml->entry as $result){ 

      if ($result->updated == $result->published) { 
        } 
    } 
} 

当我把上面的代码,它显示一个记录5次。

感谢 让

回答

1
$count = 0; 
foreach($xml->entry as $result) 
{ 
    if ($result->updated == $result->published) { 
    } 

    $count++;  
    if ($count++ == 5) break; 
    // if ($count++ == 5) break; think this might work aswell 
} 
0

看来,foreach循环只运行一次,因为只有一个entry,并且for循环打印5次。如果有多个,则此代码将打印每个条目5次。如果$xml->entry是一个数组,你可以做这样的:

for($i = 0; $i < 5; $i++) { 
    $result = $xml->entry[$i]; 

    if($result->updated == $result->published) { 
    } 
} 

检查是否有在XML文件中不止一个<entry>标签。